rombdn / img-touch-canvas

Add touch gestures (pinch zoom) to images based on Canvas (vanilla HTML5 / JS)
MIT License
148 stars 110 forks source link

Can't draw on top of img-touch-canvas #1

Closed fredericoschardong closed 11 years ago

fredericoschardong commented 11 years ago

I can't draw on top of img-touch-canvas for some reason. Here is the code: http://pastebin.com/qbTV4Njj

Am I doing something wrong or is this a bug with img-touch-canvas?

rombdn commented 11 years ago

I think you can't draw atop of the canvas because there is already an animate/refresh function (animate) which I think will prevent (or conflict with) drawing on top of the canvas (https://github.com/rombdn/img-touch-canvas/blob/master/img-touch-canvas.js#L71).

(Beside that I think your code is problematic because you are defining a new setInterval each time a touchstart event happens. You should put that setInterval part outside of the event handler function and call it only once.)

A possible workaround could be to draw your particle in the animate function of ImgTouchCanvas :

change

var gesturableImg = new ImgTouchCanvas({
    canvas: document.getElementById('img_canvas'),
    path: imgURL
});

to

var gesturableImg = new ImgTouchCanvas({
    canvas: document.getElementById('img_canvas'),
    path: imgURL,
    particle: particle
});

(put the particle definition before that call)

fredericoschardong commented 11 years ago

You are right, that setTimeout was in the wrong place.

I solved this in a more elegant way: creating a function that is called before calling requestAnimationFrame:

this.do_before_animate = options.do_before_animate; in line 24 this.do_before_animate(); before calling requestAnimationFrame

Thanks for your help!