js+canvas繪制矩形的方法
本文實(shí)例講述了js+canvas繪制矩形的方法。分享給大家供大家參考,具體如下:
運(yùn)行效果截圖如下:

具體代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>canvas繪制矩形</title>
<script type="text/javascript" >
function draw(id) {
var canvas = document.getElementById("canvas");
if (canvas) {
var context = canvas.getContext("2d");
context.fillStyle = "#DDDDDD";
context.fillRect(0, 0, 400, 400);
context.strokeStyle = "black";
context.fillStyle = "gray";
context.lineWidth = 5;
context.fillRect(0,0,200,300);
context.strokeRect(0,0,200,200);
} else {
return;
}
}
function drawBorder(id) {
var canvas = document.getElementById("canvas2");
if (canvas) {
var context = canvas.getContext("2d");
context.fillStyle = "red";
context.strokeStyle = "black";
context.lineWidth = 5;
context.fillRect(0,0, 300, 200);
context.strokeRect(0,0,300,200);
} else {
return;
}
}
window.onload = function () {
draw("canvas");
drawBorder("canvas2");
}
</script>
</head>
<body>
<canvas id="canvas" width="400" style="background:red;" height="400"></canvas>
<hr />
<canvas id="canvas2" width="400" height="400"></canvas>
</body>
</html>
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript動畫特效與技巧匯總》及《JavaScript運(yùn)動效果與技巧匯總》
希望本文所述對大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
js+html5實(shí)現(xiàn)canvas繪制鏤空字體文本的方法
這篇文章主要介紹了js+html5實(shí)現(xiàn)canvas繪制鏤空字體文本的方法,涉及html5文字效果的相關(guān)技巧,需要的朋友可以參考下2015-06-06
Echarts?3D散點(diǎn)圖實(shí)戰(zhàn)案例
這篇文章主要給大家介紹了關(guān)于Echarts?3D散點(diǎn)圖的相關(guān)資料, Echarts散點(diǎn)圖是一種常用的數(shù)據(jù)可視化圖表類型,用于展示兩個或多個維度的數(shù)據(jù)分布情況,需要的朋友可以參考下2023-11-11
關(guān)于JS通過google翻譯插件實(shí)現(xiàn)多語言版本
這篇文章主要介紹了JS通過google翻譯插件實(shí)現(xiàn)多語言版本,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
JavaScript實(shí)現(xiàn)簡單的日歷效果
本文給大家分享的是一個簡單的JavaScript制作的日歷模板,小伙伴們可以根據(jù)自己的需求,繼續(xù)補(bǔ)充,希望大家能夠喜歡2016-09-09
javascript實(shí)現(xiàn)一款好看的秒表計(jì)時器
這篇文章主要為大家詳細(xì)介紹了一款好看的秒表計(jì)時器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-09-09
JS函數(shù)的call和apply的實(shí)現(xiàn)方法區(qū)別分析
這篇文章主要為大家介紹了JS函數(shù)的call和apply的實(shí)現(xiàn)方法區(qū)別分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10

