html5 canvas ( 矩形的繪制 ) rect, strokeRect, fillRect


<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>canvas</title> <script type="text/javascript" src="../js/jQuery.js"></script> <style type="text/css"> *{ margin: 0; padding: 0; outline: none; border: none; } #canvas{ margin: auto auto; width: 7rem; margin: .25rem 0 0 1.5rem; border: 1px solid black; } </style> </head> <body> <canvas id="canvas" width="1000" height="600"></canvas> </body> </html> <script type="text/javascript"> /** * rem 布局初始化 */ $('html').css('font-size', $(window).width()/10); /** * 獲取 canvas 畫布 * 獲取 canvas 繪圖上下文環境 */ var canvas = $('#canvas')[0]; var cxt = canvas.getContext('2d'); /** * canvas 繪制矩形的方法 一 * rect(矩形的左上定點橫坐標, 矩形的左上定點縱坐標, 矩形的寬度, 矩形的高度) * 該方法只是描述了 矩形的路徑, 需要調用畫線或者填充的方法才能起作用 */ cxt.fillStyle = 'yellow'; cxt.rect(200, 100, 400, 300); //cxt.stroke();  cxt.fill(); /** * 繪制矩形的方法二 * fillRect(矩形的左上定點橫坐標, 矩形的左上定點縱坐標, 矩形的寬度, 矩形的高度) * strokeRect(矩形的左上定點橫坐標, 矩形的左上定點縱坐標, 矩形的寬度, 矩形的高度) * 這兩個方法會直接繪制出圖形 */ cxt.fillStyle = 'red'; //cxt.strokeRect(400, 200, 400, 300);  cxt.fillRect(400, 200, 400, 300); </script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM