为画布绘图添加鼠标事件
Add Mouse Events for Canvas Drawing
我需要通过Javascript将鼠标事件添加到下面的代码中。。。我已经添加了触摸事件,以便在桌面浏览器中进行测试。我需要添加鼠标事件。。我尝试将鼠标事件添加到addEventListener,但似乎不起作用。我不太确定出了什么问题。。。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=768px, maximum-scale=1.0" />
<title>rsaCanvas</title>
<script type="text/javascript" charset="utf-8">
window.addEventListener('load',function(){
// get the canvas element and its context
var canvas = document.getElementById('rsaCanvas');
var insertImage = document.getElementById('insert');
var context = canvas.getContext('2d');
//load image and annotation method
var loadData = {
imageLoad: function(){
var img = new Image();
img.src = 'the_scream.jpg';
context.drawImage(img,0,0);
}
};
// create a drawer which tracks touch movements
var drawer = {
isDrawing: false,
touchstart: function(coors){
context.beginPath();
context.moveTo(coors.x, coors.y);
this.isDrawing = true;
},
touchmove: function(coors){
if (this.isDrawing) {
context.lineTo(coors.x, coors.y);
context.stroke();
}
},
touchend: function(coors){
if (this.isDrawing) {
this.touchmove(coors);
this.isDrawing = false;
}
}
};
// create a function to pass touch events and coordinates to drawer
function draw(event){
// get the touch coordinates
var coors = {
x: event.targetTouches[0].pageX,
y: event.targetTouches[0].pageY
};
// pass the coordinates to the appropriate handler
drawer[event.type](coors);
}
// attach the touchstart, touchmove, touchend event listeners.
canvas.addEventListener('touchstart',draw, false);
canvas.addEventListener('touchmove',draw, false);
canvas.addEventListener('touchend',draw, false);
insertImage.addEventListener('click',loadData.imageLoad, false);
// prevent elastic scrolling
document.body.addEventListener('touchmove',function(event){
event.preventDefault();
},false); // end body.onTouchMove
},false); // end window.onLoad
</script>
<style>
#rsaCanvas{border:5px solid #000;}
</style>
</head>
<body>
<div id="container">
<canvas id="rsaCanvas" width="400" height="500">
Sorry, your browser is not supported.
</canvas>
<button id="insert">Insert Image</button>
</div>
</body>
</html>
试试这个。
function init () {
// ...
// Attach the mousemove event handler.
canvas.addEventListener('mousemove', ev_mousemove, false);
}
// The mousemove event handler.
var started = false;
function ev_mousemove (ev) {
var x, y;
// Get the mouse position relative to the canvas element.
if (ev.layerX || ev.layerX == 0) { // Firefox
x = ev.layerX;
y = ev.layerY;
} else if (ev.offsetX || ev.offsetX == 0) { // Opera
x = ev.offsetX;
y = ev.offsetY;
}
// The event handler works like a drawing pencil which tracks the mouse
// movements. We start drawing a path made up of lines.
if (!started) {
context.beginPath();
context.moveTo(x, y);
started = true;
} else {
context.lineTo(x, y);
context.stroke();
}
}
来源http://dev.opera.com/articles/view/html5-canvas-painting/
如果你的问题是insertImage.addEventListener('click',loadData.imageLoad, false);
在你点击时没有显示图像,那是因为
imageLoad: function(){
var img = new Image();
img.src = 'the_scream.jpg';
context.drawImage(img,0,0);
}
注意img.src是异步的,要绘制图像就要做
img.onload = (function() {
var image = img;
return function() {context.drawImage(image, 0, 0);};
})();
相关文章:
- 为画布绘图添加鼠标事件
- 将鼠标悬停在Angular JS中的一个元素上,并将Class添加到另一个元素中
- Javascript/jQuery-鼠标事件没有在html上触发,添加了动态
- 在 Adobe Animate CC 中添加鼠标悬停指针
- 高图表.js添加鼠标悬停功能或悬停状态(实现中断图表?
- 在 C# 代码隐藏中添加鼠标悬停属性
- 动态创建画布并添加鼠标按下功能
- 如果鼠标移动到图像上,我如何添加鼠标事件,它将显示播放/停止/暂停按钮
- 在jq绘图图上添加鼠标悬停
- 如何为圆形图像添加鼠标悬停阴影效果
- CSS下拉菜单:添加鼠标退出延迟
- 如何在绘制画布组件中添加鼠标事件
- HTML5.如何在绘制的画布组件中添加鼠标事件
- 在容器中放置图像和添加鼠标交互时的浏览器兼容性
- 如何捕获鼠标悬停事件时,动态添加鼠标已经在元素上
- jQuery菜单-如何添加鼠标事件
- 如何在画布的圆圈中添加鼠标单击/拖动事件.在HTML 5
- 添加鼠标上键&onMousedown事件——javascript函数内部
- (添加鼠标滚轮功能)使用DOMmouseScroll AND鼠标滚轮来调用函数
- 我想使用 JavaScript 在我的画布上添加鼠标悬停效果