所以,我正在开发HTML5中的一些软件,涉及画布的使用。有一个画布,我需要能够缩放并允许用户在鼠标点击时在画布上进行涂鸦。到目前为止,在我发现的一些例子的帮助下,我已经让缩放工作了。问题是,在缩放后,我的绘图工具上的鼠标位置不合适。在任何缩放之前,我可以画得很好。以下是缩放的代码:
//Zoom
mainCanvas.onmousewheel = function(event) {
var mousex = event.clientX - mainCanvas.offsetLeft;
var mousey = event.clientY - mainCanvas.offsetTop;
var wheel = event.wheelDelta / 120;
//n or -n
var zoom = 0;
if(wheel < 0) {
zoom = 1 / 2;
if(currentzoom == 1)
return;
} else {
mousex = event.clientX - mainCanvas.offsetLeft;
mousey = event.clientY - mainCanvas.offsetTop;
zoom = 2;
if(currentzoom == 32)
return;
}
currentzoom *= zoom;
mainContext.translate(originx, originy);
mainContext.scale(zoom, zoom);
mainContext.translate(-(mousex / scale + originx - mousex / (scale * zoom ) ), -(mousey / scale + originy - mousey / (scale * zoom ) ));
originx = (mousex / scale + originx - mousex / (scale * zoom ) );
originy = (mousey / scale + originy - mousey / (scale * zoom ) );
scale *= zoom;
draw(mainContext, gridArray);
}
就像我说的那样,缩放不是实际问题,只是问题的根源。以下是确定绘图工具鼠标位置的代码:
//this function determines the mouse position relative to the canvas element
function ev_canvas(ev) {
if(ev.layerX || ev.layerX == 0) {//Firefox, IE
ev._x = ev.layerX;
ev._y = ev.layerY;
} else if(ev.offsetX || ev.offsetX == 0) {//Opera
ev._x = ev.offsetX;
ev._y = ev.offsetY;
}
var func = tool[ev.type];
if(func) {
func(ev);
}
}
我确定问题出在后一段代码中,但我不确定要修复它。 任何帮助将不胜感激。
答案 0 :(得分:0)
我怀疑这是一段依赖于currentzoom
所以,如果我理解这个问题,那么问题在于你如何使用画布进行鼠标控制和缩放。我怀疑当你缩放时,DOM将鼠标光标放在它最初渲染的图片上。因此,如果放大200%并将鼠标放在画布中心左侧100像素处,则画布的行为就像鼠标位于中心左侧200像素一样。
currentzoom = 1;
originX = 0;
originY = 0;
function ev_canvas(ev) {
if(ev.layerX || ev.layerX == 0) {//Firefox, IE
ev._x = ev.layerX * currentzoom / 1 - originX;
ev._y = ev.layerY * currentzoom / 1 - originY;
} else if(ev.offsetX || ev.offsetX == 0) {//Opera
ev._x = ev.offsetX * currentzoom / 1 - originX;
ev._y = ev.offsetY * currentzoom / 1 - originY;
}
var func = tool[ev.type];
if(func) {
func(ev);
}
}
如果将来的用户想要将非“1”值设置为/ 1
,则会保留currentzoom
。
答案 1 :(得分:0)
对于Joomla网站,您可以使用鼠标缩放 - 它是一个Joomla扩展程序,使您可以从Joomla网页查看更大版本的缩略图。现在,您可以轻松浏览图像库:只需将鼠标光标移动到缩略图上即可查看完整大小的图像,而无需加载新页面。在此处下载http://joomlaboat.com/mouse-over-zoom
如果图片不适合窗口,则会自动调整图片大小。您可以自定义扩展,还有很多其他选项。为了加载透明图像,没有背景只需使用.png图像。
答案 2 :(得分:0)
试试这个:
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<style>
body {
margin: 0px;
padding: 0px;
}
#wrapper {
position: relative;
border: 1px solid #9C9898;
width: 578px;
height: 200px;
}
#buttonWrapper {
position: absolute;
width: 30px;
top: 2px;
right: 2px;
}
input[type =
"button"] {
padding: 5px;
width: 30px;
margin: 0px 0px 2px 0px;
}
</style>
<script>
function draw(scale, translatePos){
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
// clear canvas
context.clearRect(0, 0, canvas.width, canvas.height);
context.save();
context.translate(translatePos.x, translatePos.y);
context.scale(scale, scale);
context.beginPath(); // begin custom shape
context.moveTo(-119, -20);
context.bezierCurveTo(-159, 0, -159, 50, -59, 50);
context.bezierCurveTo(-39, 80, 31, 80, 51, 50);
context.bezierCurveTo(131, 50, 131, 20, 101, 0);
context.bezierCurveTo(141, -60, 81, -70, 51, -50);
context.bezierCurveTo(31, -95, -39, -80, -39, -50);
context.bezierCurveTo(-89, -95, -139, -80, -119, -20);
context.closePath(); // complete custom shape
var grd = context.createLinearGradient(-59, -100, 81, 100);
grd.addColorStop(0, "#8ED6FF"); // light blue
grd.addColorStop(1, "#004CB3"); // dark blue
context.fillStyle = grd;
context.fill();
context.lineWidth = 5;
context.strokeStyle = "#0000ff";
context.stroke();
context.restore();
}
window.onload = function(){
var canvas = document.getElementById("myCanvas");
var translatePos = {
x: canvas.width / 2,
y: canvas.height / 2
};
var scale = 1.0;
var scaleMultiplier = 0.8;
var startDragOffset = {};
var mouseDown = false;
// add button event listeners
document.getElementById("plus").addEventListener("click", function(){
scale /= scaleMultiplier;
draw(scale, translatePos);
}, false);
document.getElementById("minus").addEventListener("click", function(){
scale *= scaleMultiplier;
draw(scale, translatePos);
}, false);
// add event listeners to handle screen drag
canvas.addEventListener("mousedown", function(evt){
mouseDown = true;
startDragOffset.x = evt.clientX - translatePos.x;
startDragOffset.y = evt.clientY - translatePos.y;
});
canvas.addEventListener("mouseup", function(evt){
mouseDown = false;
});
canvas.addEventListener("mouseover", function(evt){
mouseDown = false;
});
canvas.addEventListener("mouseout", function(evt){
mouseDown = false;
});
canvas.addEventListener("mousemove", function(evt){
if (mouseDown) {
translatePos.x = evt.clientX - startDragOffset.x;
translatePos.y = evt.clientY - startDragOffset.y;
draw(scale, translatePos);
}
});
draw(scale, translatePos);
};
jQuery(document).ready(function(){
$("#wrapper").mouseover(function(e){
$('#status').html(e.pageX +', '+ e.pageY);
});
})
</script>
</head>
<body onmousedown="return false;">
<div id="wrapper">
<canvas id="myCanvas" width="578" height="200">
</canvas>
<div id="buttonWrapper">
<input type="button" id="plus" value="+"><input type="button" id="minus" value="-">
</div>
</div>
<h2 id="status">
0, 0
</h2>
</body>
</html>
使用鼠标移动轨迹对我来说非常完美.. Njoy !!!
答案 3 :(得分:0)
我有一个更好的脚本来在画布上获得鼠标位置:
function writeMessage(canvas, message) {
var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
context.font = '18pt Calibri';
context.fillStyle = 'black';
context.fillText(message, 10, 25);
}
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect(), root = document.documentElement;
// return relative mouse position
var mouseX = evt.clientX - rect.top - root.scrollTop;
var mouseY = evt.clientY - rect.left - root.scrollLeft;
return {
x: mouseX,
y: mouseY
};
}
window.onload = function() {
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
canvas.addEventListener('mousemove', function(evt) {
var mousePos = getMousePos(canvas, evt);
var message = "Mouse position: " + mousePos.x + "," + mousePos.y;
writeMessage(canvas, message);
}, false);
};
尝试一下,评论是否有任何问题..