2014-12-31 70 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