Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6988165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:02:22+00:00 2026-05-27T19:02:22+00:00

In an application i’m working on it should be possible to open several div

  • 0

In an application i’m working on it should be possible to open several div boxes and draw on each of them (like MS paint light).

I have succeeded open one box and draw on it but when I changed from canvas “id” to canvas “class” (to be able to open several boxes and paint on each of them) it stopped working. Now when I try to paint, nothing happens. What am I doing wrong?

var color = "red";
var shape = "square";
var size = 10;

var theWindow = $('.window').last();
var bottomWindow = $('.windowBottom').last();

var letsdraw = false;
var theCanvas = $('.paint').last();

var ctx = theCanvas.getContext('2d');
ctx.strokeStyle = 'red';
theCanvas.width  = 430;
theCanvas.height = 317;

var canvasOffset = $(theCanvas).offset();

$(theCanvas).mousemove(function(e) {
    if (letsdraw === true) {
        ctx.lineTo(e.pageX - canvasOffset.left, e.pageY - canvasOffset.top);
        ctx.stroke();
    }
});

$(theCanvas).mousedown(function(e) {
    letsdraw = true;
    ctx.strokeStyle = color;
    ctx.lineWidth = size;
    ctx.lineCap = 'round';
    ctx.beginPath();
    ctx.moveTo(e.pageX - canvasOffset.left, e.pageY - canvasOffset.top);
});

$(window).mouseup(function() {
    letsdraw = false;
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:02:24+00:00Added an answer on May 27, 2026 at 7:02 pm

    This problem is you are still treating it as one canvas.

    $('.paint').mousemove(function(e) {
        if (letsdraw === true) {
            var ctx = this.getContext('2d');
            var canvasOffset = $(this).offset();
            ctx.lineTo(e.pageX - canvasOffset.left, e.pageY - canvasOffset.top);
            ctx.stroke();
        }
    });
    

    So I removed theCanvas and using this to reference the canvas. Click the demo link to see it in action:

    Demo


    Some more edits using the data attr:

    // using data attr to store lets draw per canvas
    $(window).mouseup(function() {
        $('.paint').each(function () {
           $(this).data('letsdraw', false); 
        });
    });
    
    $('.paint').mousemove(function(e) {
        if ($(this).data('letsdraw') === true) {
            var ctx = this.getContext('2d');
            var canvasOffset = $(this).offset();
            ctx.lineTo(e.pageX - canvasOffset.left, e.pageY - canvasOffset.top);
            ctx.stroke();
        }
    });
    

    Changing the height and width:

    $('.paint').each(function () {
        this.width = 430;
        this.height = 20
    })
    

    I only do this once, because changing the height/width of a canvas actually erases all drawing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Application[temp] = 8; should set the value 8 to the key temp . However,
Application I work on generates several hundreds of files (csv) in a 15 minutes
Application : I am working on one mid-large size application which will be used
Application is a C# .Net 3.5 WCF Service. I'd like during the build process
APPLICATION DESCRIPTION : I am a new iPhone developer. I am working on an
My application scenario is like this: I want to evaluate the performance gain one
Application I develop requires several data sources (2 RDBMS and one file storage) to
Application I am working on an MMO and have run into an issue. The
Application able to record error in OnError, but we are not able to do
Application frameworks such as DotNetNuke, Eclipse, Websphere and so forth are available today which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.