My snapping works in Firefox only, any legal code?
I'm learning JS (think I'm not too old to learn) and coding app for drawing electronics at: http://3lectronics.com/draw/Atarado-Draw1.html
The most powerful help I can get is here, because of many great guys sharing knowledge. I stacked cause of 20x20px snapping to grid because I found some hacky (obviously) way to make those snapping. It is working only with Firefox and Seamonkey...code:
function ev_canvas (ev) {
if (ev.layerX || ev.layerX == 0) { // Firefox
x2 = (ev.layerX /2).toFixed(-1) *2 ;
y2 = (ev.layerY /2).toFixed(-1) *2 ;
} else if (ev.offsetX || ev.offsetX == 0) { // Opera
x2 = ev.offsetX;
y2 = e开发者_开发问答v.offsetY;
}
It works excellent (you can try), but other browsers disobey to run it. I found that Chrome doesn't like negative value in .toFixed(), and without this it works, but I don't have snapping... Any idea....Thanx.
toFixed is only supported between 0 and 20 in Chrome
Another way to round to the 10's place would be to divide by 10, round, and than multiply by 10.
var x2 = Math.round(ev.layerX /20) *20 ;
Not sure what is up with the *2
/2
you have going on, but I left it in there.
精彩评论