2014-12-31 47 views
0

我有以下的帆布代码: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; 似乎是造成问题的原因之一。

+1

当我创建一个捣鼓它的一切似乎工作,请在** **什么是不工作... –

+0

您好,在我的HTML我没有指定更具体宽度和高度,我认为这是造成问题的原因。 – user4269367

+0

当我给它一个百分比的宽度/高度时,画布停止工作,当我使用像素时,它可以工作,但绘图已经关闭了一点,目前您可以看到画布的宽度/高度由类图像决定使用百分比 – user4269367

回答

0

代码对我来说似乎没问题。我添加了对init函数的调用。

由于LJ_1102声明: 补偿问题是一个CSS问题。 The width and height attributes set the width and height of the bitmap itself. CSS宽度和高度属性设置元素的宽度和高度。因此,请考虑canvas标签,就好像它是img标签。如果您设置img的CSS宽度,它会拉伸。画布将做同样的事情。

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(); 
 
    } 
 
    } 
 
} 
 
init();
<div class="row 50% third"> 
 
    <div class="12u"> 
 
    <canvas class="image fit" id="can" width="50%" height="50%" 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>

+0

这是独特的,代码片段看起来像它的工作与百分比的宽度和高度,但似乎并没有对我的功能。会做更多的测试 – user4269367

+0

@ user4269367我刚刚在IE,Firefox和Chrome上测试过。我改变的唯一的事情是,我添加了百分比宽度来显示百分比工作,并将调用添加到init()。没有init()调用你的画布将不会被设置。 – teynon

+0

嗨我的初始化调用是在身上onload,这也会工作吗? – user4269367