<!--
/*************************************************************
* This script is developed by Arturs Sosins aka ar2rsawseen, http://webcodingeasy.com
* Feel free to distribute and modify code, but keep reference to its creator
*
* Canvas Events class extends canvas object to help to attach mouse events
* to different shapes with minimal javascript code modifications.
* Canvas context methods that perform actual drawing like
* stroke, fill, strokeRect, fillRect, drawImage
* return a shape object to which you can attach events
*
* For more information, examples and online documentation visit:
* http://webcodingeasy.com/JS-classes/Emulate-events-on-canvas-objects
**************************************************************/
-->
<html>
<head>
</head>
<body>
<canvas id='canvas' width='800' height='600'></canvas>
<p id='debug'></p>
<script src="./canvas_events.packed.js" type="text/javascript"></script>
<script>
//canvas
var c = document.getElementById("canvas");
//ctx
var ctx = new canvas_events("canvas");
//modify coordinates
var modX = 0;
var modY = 0;
var rect = ctx.fillRect(50,50,100,100);
rect.addEvent("mouseover", function(e,args){
this.recreate(args);
this.strokeStyle = "red";
this.stroke();
c.style.cursor = "pointer";
c.style.cursor = "hand";
});
rect.addEvent("mouseout", function(e,args){
this.clearRect(0,0,800,600);
this.recreate(args);
this.fill();
c.style.cursor = "default";
});
rect.addEvent("mousedown", function(e,args){
modX = e.elemX;
modY = e.elemY;
rect.addEvent("mousemove", function(e,args){
this.clearRect(0,0,800,600);
this.recreate(args, e.elemX - modX, e.elemY - modY);
modX = e.elemX;
modY = e.elemY;
this.fill();
});
});
rect.addEvent("mouseup", function(e,args){
rect.mousemove = null;
});
</script>
</body>
</html> |