1. 程式人生 > >(練習題)canvas畫板(簡)

(練習題)canvas畫板(簡)

play dev off etl event html -s span line

canvas:IE9及以上版本,主流瀏覽器都支持。

註意:當需要設定canvas的高度時不要寫在css中,canvas默認是300x150,在css中設置寬高相當於等比放大或者縮小了,canvas中的內容也會等比放大或者縮小。

<!DOCTYPE html>
<html lang="">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <style>
        #canvas {
            display: block;
            margin: 
0 auto; border: 1px solid #000; } </style> </head> <body> <canvas id="canvas" width="600" height="600"></canvas> <script> var canvas = document.getElementById(‘canvas‘); var context = canvas.getContext(‘2d‘); canvas.onmousedown
= function(ev) { var ev = ev || window.event; context.moveTo(ev.clientX - canvas.offsetLeft, ev.clientY - canvas.offsetTop); document.onmousemove = function(ev) { var ev = ev || window.event; context.strokeStyle = ‘yellow‘ context.lineTo(ev.clientX
- canvas.offsetLeft, ev.clientY - canvas.offsetTop); context.stroke(); } document.onmouseup = function() { document.onmousemove = null; document.onmousedown = null; } } </script> </body> </html>

(練習題)canvas畫板(簡)