每天一個JavaScript執行個體-canvas畫圖,javascript-canvas

來源:互聯網
上載者:User

每天一個JavaScript執行個體-canvas畫圖,javascript-canvas

<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>每天一個JavaScript執行個體-canvas畫圖</title><style>.canvas{width:600px;height:500px;}</style><script>window.onload = function(){var imgcanvas = document.getElementById("imgcanvas");if(imgcanvas.getContext){var ctx = imgcanvas.getContext('2d');ctx.fillStyle="rgba(255,0,0,.1)";ctx.strokeStyle = "#000";//第一個ctx.fillRect(0,0,100,100);ctx.strokeRect(0,0,100,100);//第二個ctx.fillRect(50,50,100,200);//第三個ctx.strokeRect(80,130,200,100);}}</script></head><body><canvas class="canvas" id = "imgcanvas"><p>畫布</p></canvas></body></html>


html 5中,怎用javascript在canvas裡畫出一個笑臉?

利用基本路徑API
注意最後fill或stroke上去後之前緩衝的路徑資訊才會繪製到canvas上去
function drawShape(){
var canvas = document.getElementById('你的canvas的ID');
if (canvas.getContext){
var ctx = canvas.getContext('2d');

ctx.beginPath();
ctx.arc(75,75,50,0,Math.PI*2,true); // 外圓
ctx.moveTo(110,75);
ctx.arc(75,75,35,0,Math.PI,false); // 嘴
ctx.moveTo(65,65);
ctx.arc(60,65,5,0,Math.PI*2,true); //左眼
ctx.moveTo(95,65);
ctx.arc(90,65,5,0,Math.PI*2,true); // 右眼
ctx.stroke();

} else {
alert('你的瀏覽器不支援html5,請用chrome或Firefoxsafari .');
}
}
 
javascript在canvas中定義一個用來顯示的類,產生執行個體之後,怎來對其執行相應的滑鼠事件

這個只能給canvas添加事件,然後判斷點擊的時候的位置是否和矩形重疊
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.