在 HTM5 canvas 上制作鼠标悬停时图像缩放效果>

javascriptweb developmentfront end scripts

要制作鼠标悬停时图像缩放效果,请使用 Vanilla JavaScript 库。

鼠标移动时,按如下方式设置:

function move(e) {
   var pos = getMousePos(myCanvas, e);
   context.drawImage(img, -pos.x, -pos.y, img.width, img.height);
}

对于画布:

//添加我们需要的事件监听器
myCanvas.addEventListener('mouseout', display, false);
myCanvas.addEventListener('mousemove', move, false);
function display() {
   context.drawImage(img, 0, 0, img.width>>1, img.height>>1);
}

相关文章