【发布时间】:2018-06-27 17:43:03
【问题描述】:
我正在尝试制作一个可以调整大小的绘图画布。 我找到了一种方法,但是每次更新画布时都会删除绘图,不幸的是它有一个奇怪的“错误”。 在 Firefox 中一切正常,但是当我在 Chrome 中尝试时,我无法调整画布的大小。
如果它不是最漂亮或最有效的代码,我深表歉意。
$(document).ready(function() {
$("#canvaSizer").mouseup(function() {
canvaWidthNew = document.getElementById('canvaSizer').clientWidth;
if (canvaWidthNew != canvaWidth) {
initialize();
}
canvaWidth = canvaWidthNew;
});
canvaWidth = document.getElementById('canvaSizer').clientWidth;
var myscreen = document.getElementById("screen");
var ctx = myscreen.getContext("2d");
initialize();
function initialize() {
// Fill Window Width and Height
myscreen.width = document.getElementById('canvaSizer').clientWidth;
myscreen.height = document.getElementById('canvaSizer').clientHeight;
// Set Background Color
ctx.fillStyle = "#fff";
ctx.fillRect(0, 0, myscreen.width, myscreen.height);
// Mouse Event Handlers
if (myscreen) {
var isDown = false;
var canvasX, canvasY;
ctx.lineWidth = 5;
$(myscreen)
.mousedown(function(e) {
isDown = true;
ctx.beginPath();
canvasX = e.pageX - myscreen.offsetLeft;
canvasY = e.pageY - myscreen.offsetTop;
ctx.moveTo(canvasX, canvasY);
})
.mousemove(function(e) {
if (isDown !== false) {
canvasX = e.pageX - myscreen.offsetLeft;
canvasY = e.pageY - myscreen.offsetTop;
ctx.lineTo(canvasX, canvasY);
ctx.strokeStyle = "#000";
ctx.stroke();
}
})
.mouseup(function(e) {
isDown = false;
ctx.closePath();
});
}
// Touch Events Handlers
draw = {
started: false,
start: function(evt) {
ctx.beginPath();
ctx.moveTo(
evt.touches[0].pageX,
evt.touches[0].pageY
);
this.started = true;
},
move: function(evt) {
if (this.started) {
ctx.lineTo(
evt.touches[0].pageX,
evt.touches[0].pageY
);
ctx.strokeStyle = "#000";
ctx.lineWidth = 5;
ctx.stroke();
}
},
end: function(evt) {
this.started = false;
}
};
// Touch Events
myscreen.addEventListener('touchstart', draw.start, false);
myscreen.addEventListener('touchend', draw.end, false);
myscreen.addEventListener('touchmove', draw.move, false);
// Disable Page Move
document.body.addEventListener('touchmove', function(evt) {
evt.preventDefault();
}, false);
}
});
* {
margin: 0;
padding: 0;
}
html,
body {
width: 100%;
height: 100%;
overflow: hidden;
}
#canvasWrapper {
width: 95.6%;
height: 100%;
position: absolute;
z-index: 1;
overflow: hidden;
}
#canvaSizer {
padding: 0;
display: block;
width: 35%;
border-style: solid;
height: 35%;
position: absolute;
resize: both;
max-width: 95%;
min-width: 35%;
max-height: 95%;
min-height: 35%;
overflow: hidden;
resize: both;
}
canvas {}
#screen {
cursor: crosshair;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<body>
<div id="canvasWrapper">
<div id="canvaSizer">
<canvas id="screen"></canvas>
</div>
</div>
</body>
</html>
【问题讨论】:
-
请在 firefox 和 chrome 中尝试 sn-p
-
看起来好像画布被渲染在canvaSizer之上,因此无法访问。如果您更改调整大小代码以为调整大小部分留出空间,这似乎可行(尽管可能不是可接受的解决方法)。即改变 myscreen.height = document.getElementById('canvaSizer').clientHeight; TO myscreen.height = document.getElementById('canvaSizer').clientHeight - 20;
标签: javascript jquery html google-chrome canvas