Click here to Skip to main content
16,004,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i dynamically made a canvas as so:

C#
function pageSetup() {
            var vDuration = 30;
            var vHeight = 280;
            var canvas = document.createElement('canvas');
                        canvas.id = "canvas0";
            canvas.width  = 60;
            canvas.height = vHeight;
            canvas.style.position = "absolute";

            for(var canCount = 0; canCount < vDuration; canCount++)
                        {
                                var ctx=canvas.getContext("2d");
                ctx.stroke();
                ctx.fillStyle="#FF0000";
                var topX = 0;
                var topY = canCount*(vHeight/vDuration);
                var botX = 40;
                var botY = topX + (vHeight/vDuration);
                ctx.fillRect(topX, topY, botX, botY);

                        }


It makes a canvas 280 pixels high and 30 wide. Within it, 30 rectangles are drawn in a vertical stack from top to bottom. Each has a width of 40. The total canvas width it 60 however.

What I want now is when the user hovers over one of these rectangles it expands (or rather theyre redrawn with a new one 60 pixels wide instead of 40 where the mouse is. For some reason the following wont work:

C#
$(document).on("mouseover", "#canvas", function(e) {

              var ctx = $("#canvas0")[0].getContext('2d');
              ctx.fillStyle="#000000";
              ctx.fillRect(0,0,30,30 );

});



if i can get it to draw a rectangle on a user hover then i can just adjust where it draws the new extra length rectangle

Tried also:

C#
$('#myCanvas').mousemove(function (e) {
    // Event location offset - the offset of the element = offset within layer
    var context = document.getElementById("canvasId").getContext("2d");
    context.clearRect(0,0,context..canvas.width,context.canvas.height);
    context.fillStyle="#000000";
    context.fillRect(0,0,50,50);

});
Posted
Updated 8-Jan-14 3:52am
v2
Comments
lostandconfused1234 8-Jan-14 8:50am    
NOTE: If you know a better way than canvas I would change code. Thanks

1 solution

If you really want to use Canvas for this purpose then i suggest this library:
http://kineticjs.com[^]

OR you can achieve the same effect using simple javascirpt, use 'div' to represent blocks and attach onmouse over events.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900