我有以下的帆布代碼:HTML畫布不工作
var canvas, ctx, flag = false,
prevX = 0,
currX = 0,
prevY = 0,
currY = 0,
dot_flag = false;
var x = "black",
y = 2;
function init() {
canvas = document.getElementById('can');
ctx = canvas.getContext("2d");
w = canvas.width;
h = canvas.height;
canvas.addEventListener("mousemove", function(e) {
findxy('move', e)
}, false);
canvas.addEventListener("mousedown", function(e) {
findxy('down', e)
}, false);
canvas.addEventListener("mouseup", function(e) {
findxy('up', e)
}, false);
canvas.addEventListener("mouseout", function(e) {
findxy('out', e)
}, false);
}
function color(obj) {
switch (obj.id) {
case "blue":
x = "blue";
break;
case "red":
x = "red";
break;
case "black":
x = "black";
break;
case "white":
x = "white";
break;
}
if (x == "white") y = 14;
else y = 2;
}
function draw() {
ctx.beginPath();
ctx.moveTo(prevX, prevY);
ctx.lineTo(currX, currY);
ctx.strokeStyle = x;
ctx.lineWidth = y;
ctx.stroke();
ctx.closePath();
}
function erase() {
var m = confirm("Want to clear");
if (m) {
ctx.clearRect(0, 0, w, h);
document.getElementById("canvasimg").style.display = "none";
}
}
function save() {
document.getElementById("canvasimg").style.border = "2px solid";
var dataURL = canvas.toDataURL();
document.getElementById("canvasimg").src = dataURL;
document.getElementById("canvasimg").style.display = "inline";
}
function findxy(res, e) {
if (res == 'down') {
prevX = currX;
prevY = currY;
currX = e.clientX - canvas.offsetLeft;
currY = e.clientY - canvas.offsetTop;
flag = true;
dot_flag = true;
if (dot_flag) {
ctx.beginPath();
ctx.fillStyle = x;
ctx.fillRect(currX, currY, 2, 2);
ctx.closePath();
dot_flag = false;
}
}
if (res == 'up' || res == "out") {
flag = false;
}
if (res == 'move') {
if (flag) {
prevX = currX;
prevY = currY;
currX = e.clientX - canvas.offsetLeft;
currY = e.clientY - canvas.offsetTop;
draw();
}
}
}
<div class="row 50% third">
<div class="12u">
<canvas class="image fit" id="can" style=" background-color:rgb(250,250,250); "></canvas>
<div style="width:5%;height:30px;background:blue;" id="blue" class="colors" onclick="color(this)"></div>
<div style="width:5%;height:30px;background:red;" id="red" class="colors" onclick="color(this)"></div>
<div style="width:5%;height:30px;background:black;" id="black" class="colors" onclick="color(this)"></div>
<div style="width:5%;height:30px;background:white;border:2px solid;" id="white" class="colors" onclick="color(this)"></div>
<img id="canvasimg" style="position:absolute;top:10%;left:52%;" style="display:none;">
<input type="button" value="save" id="btn" size="30" onclick="save()" style="position:absolute;top:55%;left:10%;">
<input type="button" value="clear" id="clr" size="23" onclick="erase()" style="position:absolute;top:55%;left:15%;">
</div>
</div>
我已經把我的身體的onload給init();
但是,畫布不起作用。
編輯:當我在html中提供特定的寬度和高度時,畫布起作用,但是我發現我的繪圖和鼠標始終關閉一點點。還有一種方法可以使用百分比寬度和高度嗎? w = canvas.width; h = canvas.height; 似乎是造成問題的原因之一。
當我創建一個搗鼓它的一切似乎工作,請在** **什麼是不工作... –
您好,在我的HTML我沒有指定更具體寬度和高度,我認爲這是造成問題的原因。 – user4269367
當我給它一個百分比的寬度/高度時,畫布停止工作,當我使用像素時,它可以工作,但繪圖已經關閉了一點,目前您可以看到畫布的寬度/高度由類圖像決定使用百分比 – user4269367