Skip to content
Snippets Groups Projects
Commit f166654b authored by srosse's avatar srosse
Browse files

OO-4644: reactive point events, remove document listener, stop 2x stop

parent b6573d5b
No related branches found
No related tags found
No related merge requests found
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
var usePointerApi = false; var usePointerApi = false;
if (window.PointerEvent) { if (window.PointerEvent) {
usePointerApi = false; usePointerApi = true;
} }
var useRequestAnimationFrame = false; var useRequestAnimationFrame = false;
...@@ -113,7 +113,7 @@ ...@@ -113,7 +113,7 @@
var startEvents = usePointerApi ? 'pointerdown' : 'mousedown touchstart'; var startEvents = usePointerApi ? 'pointerdown' : 'mousedown touchstart';
var leaveEvents = usePointerApi ? 'pointerleave' : 'mouseleave'; var leaveEvents = usePointerApi ? 'pointerleave' : 'mouseleave';
var enterEvents = usePointerApi ? 'pointerenter' : 'mouseenter'; var enterEvents = usePointerApi ? 'pointerenter' : 'mouseenter';
var stopLeaveEvents = usePointerApi ? 'pointerup click' : 'mouseup touchend'; var stopLeaveEvents = usePointerApi ? 'pointerup' : 'mouseup touchend';
var stopEvents = usePointerApi ? 'pointerup click' : 'mouseup click touchend'; var stopEvents = usePointerApi ? 'pointerup click' : 'mouseup click touchend';
var moveEvents = usePointerApi ? 'pointermove' : 'mousemove touchmove'; var moveEvents = usePointerApi ? 'pointermove' : 'mousemove touchmove';
...@@ -123,12 +123,15 @@ ...@@ -123,12 +123,15 @@
}); });
/* Mouse Capturing Work out of the canvas */ /* Mouse Capturing Work out of the canvas */
jQuery(document).on(moveEvents, function(e) { jQuery(document).on(moveEvents, function catchMouseLeave(e) {
if(mouse.leave) { if(mouse.leave) {
var rect = tmp_canvas.getBoundingClientRect(); var rect = tmp_canvas.getBoundingClientRect();
mouse.out_x = e.clientX - rect.left; mouse.out_x = e.clientX - rect.left;
mouse.out_y = e.clientY - rect.top; mouse.out_y = e.clientY - rect.top;
} }
if(jQuery("#" + sketchId).length == 0) {
jQuery(document).off(moveEvents, catchMouseLeave);
}
}); });
jQuery(this.canvas).on(moveEvents, function(e) { jQuery(this.canvas).on(moveEvents, function(e) {
...@@ -171,9 +174,10 @@ ...@@ -171,9 +174,10 @@
if(tool == "brush") { if(tool == "brush") {
ppts.push({x: -1, y: -1}); ppts.push({x: -1, y: -1});
} }
jQuery(document).on(stopLeaveEvents, function(e){ jQuery(document).on(stopLeaveEvents, function stopListener() {
stopPainting(); stopPainting();
jQuery(document).off(stopLeaveEvents, stopListener);
}); });
}); });
...@@ -204,8 +208,10 @@ ...@@ -204,8 +208,10 @@
}; };
var stopPainting = function() { var stopPainting = function() {
jQuery(tmp_canvas).off(moveEvents, onPaint); if(!mouse.paint) return;
mouse.paint = false;
jQuery(tmp_canvas).off(moveEvents, onPaint);
// for erasing // for erasing
ctx.globalCompositeOperation = 'source-over'; ctx.globalCompositeOperation = 'source-over';
//spraying tool. //spraying tool.
...@@ -271,11 +277,11 @@ ...@@ -271,11 +277,11 @@
modal += ' </div>'; modal += ' </div>';
modal += '</div>'; modal += '</div>';
jQuery("body").append(modal); jQuery("body").append(modal);
$('#paintModal').modal('show'); jQuery('#paintModal').modal('show');
$('#paintModal button.btn-primary').on('click', function() { jQuery('#paintModal button.btn-primary').on('click', function() {
ctx.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height); ctx.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height);
}); });
$('#paintModal').on('hidden.bs.modal', function (event) { jQuery('#paintModal').on('hidden.bs.modal', function (event) {
jQuery("#paintModal").remove(); jQuery("#paintModal").remove();
}); });
o_scrollToElement('#o_top'); o_scrollToElement('#o_top');
...@@ -313,6 +319,10 @@ ...@@ -313,6 +319,10 @@
}; };
var drawPaintBrush = function() { var drawPaintBrush = function() {
if(!mouse.paint) {
return;
}
// Tmp canvas is always cleared up before drawing. // Tmp canvas is always cleared up before drawing.
tmp_ctx.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height); tmp_ctx.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height);
...@@ -345,6 +355,10 @@ ...@@ -345,6 +355,10 @@
}; };
var onPaintLine = function() { var onPaintLine = function() {
if(!mouse.paint) {
return;
}
// Tmp canvas is always cleared up before drawing. // Tmp canvas is always cleared up before drawing.
tmp_ctx.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height); tmp_ctx.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height);
...@@ -356,6 +370,10 @@ ...@@ -356,6 +370,10 @@
}; };
var onPaintCircle = function() { var onPaintCircle = function() {
if(!mouse.paint) {
return;
}
// Tmp canvas is always cleared up before drawing. // Tmp canvas is always cleared up before drawing.
tmp_ctx.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height); tmp_ctx.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height);
...@@ -364,15 +382,19 @@ ...@@ -364,15 +382,19 @@
var x = (mx + start_mouse.x) / 2; var x = (mx + start_mouse.x) / 2;
var y = (my + start_mouse.y) / 2; var y = (my + start_mouse.y) / 2;
var radius = Math.max(Math.abs(mx - start_mouse.x), Math.abs(my - start_mouse.y)) / 2; var circleRadius = Math.max(Math.abs(mx - start_mouse.x), Math.abs(my - start_mouse.y)) / 2;
tmp_ctx.beginPath(); tmp_ctx.beginPath();
tmp_ctx.arc(x, y, radius, 0, Math.PI*2, false); tmp_ctx.arc(x, y, circleRadius, 0, Math.PI*2, false);
tmp_ctx.stroke(); tmp_ctx.stroke();
tmp_ctx.closePath(); tmp_ctx.closePath();
}; };
var onPaintRect = function() { var onPaintRect = function() {
if(!mouse.paint) {
return;
}
// Tmp canvas is always cleared up before drawing. // Tmp canvas is always cleared up before drawing.
tmp_ctx.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height); tmp_ctx.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height);
...@@ -386,7 +408,11 @@ ...@@ -386,7 +408,11 @@
tmp_ctx.strokeRect(x, y, width, height); tmp_ctx.strokeRect(x, y, width, height);
}; };
function onPaintEllipse(ctx) { function onPaintEllipse() {
if(!mouse.paint) {
return;
}
tmp_ctx.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height); tmp_ctx.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height);
var mx = mouse.leave ? mouse.out_x : mouse.x; var mx = mouse.leave ? mouse.out_x : mouse.x;
...@@ -417,6 +443,10 @@ ...@@ -417,6 +443,10 @@
} }
var onErase = function() { var onErase = function() {
if(!mouse.paint) {
return;
}
// Saving all the points in an array // Saving all the points in an array
ppts.push({x: mouse.x, y: mouse.y}); ppts.push({x: mouse.x, y: mouse.y});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment