javascript - lineto draws relative to window not canvas -


i'm experimenting using drawing function on canvas , curious see if can suggest solution i'm new ;)

my main html pages have couple of canvases set up, in layers can manipulate , erase each without interfering others. have this:

<div> <canvas id="mycanvas" width="450" height="450" style="border:1px solid #d3d3d3;"></canvas> <canvas id="layer1" width="450" height="450" style="position: absolute; left:0; top:0; z-index:1;"></canvas> <canvas id="layer2" width="450" height="450" style="position: absolute; left:0; top:0; z-index:2;"></canvas> <canvas id="layer3" width="450" height="450" style="position: absolute; left:0; top:0; z-index:3;"></canvas> <canvas id="layer4" width="450" height="450" style="position: absolute; left:0; top:0; z-index:4;"></canvas> </div> 

in bit of javascript, manipulating these canvases such as:

c1=document.getelementbyid("layer1"); ctx1=c1.getcontext("2d"); ctx1.beginpath(); ctx1.moveto(this.xcoord, this.ycoord); ctx1.lineto(newx, newy); ctx1.stroke(); 

now works; draws lines on screen no problem, except not being drawn on canvas, being drawn relative main browser window (my canvases not @ top left of screen little way down due text , other graphics). can suggest doing wrong?

it realtive browser because told relative browser. need make wrapper element's position relative.

css:

div.wrapper { position:relative;} div.wrapper canvas { position : absolute; } 

html:

<div class="wrapper">     <canvas id="mycanvas" width="450" height="450" style="border:1px solid #d3d3d3;"></canvas>     <canvas id="layer1" width="450" height="450" style="z-index:1;background-color: red"></canvas>     <canvas id="layer2" width="450" height="450" style="z-index:2;background-color:yellow;"></canvas> </div> 

jsfiddle:

http://jsfiddle.net/x8bcp/1/


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -