JavaScript html5 canvas畫布中刪除一個塊區(qū)域的方法
更新時間:2016年01月26日 14:40:41 作者:m1870164
這篇文章主要介紹了JavaScript html5 canvas畫布中刪除一個塊區(qū)域的方法,涉及JavaScript結(jié)合html5操作canvas畫布圖形繪制的技巧,需要的朋友可以參考下
本文實例講述了html5 canvas畫布中刪除一個塊區(qū)域的方法。分享給大家供大家參考,具體如下:
運行效果截圖如下:

附:圖中,黑色小方塊即為刪除掉的塊區(qū)域
具體代碼如下:
index.html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>canvas中刪除一塊區(qū)域</title>
<style type="text/css">
#canvas {
background:black; margin-top:100px; margin-left:200px;
}
</style>
</head>
<body>
<canvas id="canvas" width="500px" height="500px" ></canvas>
</body>
<script type="text/javascript" src="canvas.js"></script>
<script type="text/javascript">
cache = {};
var offsetX = 50,
offsetY = 20;
cache.context = dyl.createContext('canvas');
dyl.rect(dyl.createColor(), 150, 150, 200, 200, 0.5);
for(var i=0; i<10; i++) {
dyl.clearRect(150 + i*20, 150 + i*20, 20, 20);
}
</script>
</html>
canvas.js:
(function() {
var dyl = {cache: {}};
dyl.setContext = function(context) {
dyl.cache._context = context;
return context;
};
dyl.getDom = function(id) {
return document.getElementById(id);
};
dyl._getContext = function() {
return dyl.cache._context;
};
dyl.save = function() {
var context = dyl._getContext();
context ? context.save() : void(0);
};
dyl.restore = function() {
var context = dyl._getContext();
context ? context.restore() : void(0);
};
dyl.createContext = function(canvasID) {
var canvas = this.getDom(canvasID);
if(!canvas) {
return null;
}
return dyl.setContext(canvas.getContext("2d"));
};
dyl.createColor = function() {
var color = "rgb(";
color += Math.round(Math.random()*255);
color += ",";
color += Math.round(Math.random()*255);
color += ",";
color += Math.round(Math.random()*255);
color += ")";
return color;
};
dyl.addImg = function(img, x, y) {
var context = dyl._getContext();
if(!img || !context) {
return;
}
if(typeof img === "string") {
var oImg = new Image();
oImg.src = img;
oImg.onload = function() {
context.drawImage(oImg, x, y);
}
return;
}
context.drawImage(img, x, y);
};
dyl.rect = function(color, x, y, width, height, alpha) {
var context = dyl._getContext();
if(!context) {
return;
}
context.save();
context.fillStyle = color;
context.globalAlpha = alpha ? alpha : 1;
context.fillRect(x, y, width, height);
context.restore();
};
dyl.circle = function(color, x, y, r, alpha) {
var context = dyl._getContext();
context.save();
context.fillStyle = color;
context.beginPath();
context.globalAlpha = alpha ? alpha : 1;
context.arc(x, y, r, 0, 2*Math.PI);
context.fill();
context.stroke();
};
dyl.clearRect = function(x, y, width, height) {
var context = dyl._getContext();
if(!context) {
return;
}
context.clearRect(x, y, width, height);
};
dyl.scale = function(x, y) {
var context = dyl._getContext();
if(!context) {
return;
}
x = x ? x : 1;
y = y ? y : 1;
context.scale(x, y);
};
if(!window.dyl) {
window.dyl = dyl;
}
})();
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
JavaScript利用split函數(shù)按規(guī)定截取字符串(獲取郵箱用戶名)
這個其實就是利用了js的split函數(shù),以@分割數(shù)組,一般用這個的地方不多,但這個思路應(yīng)用的比較廣泛。推薦大家學習。2009-12-12
使用TypeScript?V8來改進您的JavaScript代碼
TypeScript?V8是一個強大的JavaScript類型系統(tǒng),它可以幫助你發(fā)現(xiàn)JavaScript代碼中的錯誤和潛在問題,并在編譯時捕獲它們,以便您可以解決它們,TypeScript?V8為JavaScript提供一系列的類型注釋,包括自定義類型和其他高級功能2023-08-08
js實現(xiàn)的四級左側(cè)網(wǎng)站分類菜單實例
這篇文章主要介紹了js實現(xiàn)的四級左側(cè)網(wǎng)站分類菜單,實例分析了javascript操作頁面元素實現(xiàn)tab切換的相關(guān)技巧,需要的朋友可以參考下2015-05-05
javascript實現(xiàn)數(shù)字+字母驗證碼的簡單實例
本篇文章只要是對javascript實現(xiàn)數(shù)字+字母驗證碼的簡單實例進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02

