主頁 > 知識庫 > HTML5 Canvas自定義圓角矩形與虛線示例代碼

HTML5 Canvas自定義圓角矩形與虛線示例代碼

熱門標(biāo)簽:臨海地圖標(biāo)注app 百度地圖標(biāo)注為什么總是封號 小朱地圖標(biāo)注 四川移動電銷外呼客戶管理系統(tǒng) 智能芯電話機器人 400開頭的電話好申請不 怎么做百度地圖標(biāo)注 咸陽穩(wěn)定外呼系統(tǒng)軟件 地圖標(biāo)注柱狀圖
HTML5 Canvas自定義圓角矩形與虛線(RoundedRectangle and Dash Line)

實現(xiàn)向HTML Canvas 2d context繪制對象中添加自定義的函數(shù)功能演示,如何繪制虛線以及控制虛線間隔大小,學(xué)會繪制圓角矩形的技巧。

HTML5 Canvas繪制對象中提供的原生功能沒有實現(xiàn)繪制圓角矩形與虛線的功能,但是通過JavaScript語言的Object.prototype可以實現(xiàn)對對象CanvasRenderingContext2D添加這兩個函數(shù)功能。代碼的演示效果如下:
 
組件fishcomponent.js的代碼如下:

復(fù)制代碼
代碼如下:

CanvasRenderingContext2D.prototype.roundRect =
function(x, y, width, height, radius, fill, stroke) {
if (typeof stroke == "undefined") {
stroke = true;
}
if (typeof radius === "undefined") {
radius = 5;
}
this.beginPath();
this.moveTo(x + radius, y);
this.lineTo(x + width - radius, y);
this.quadraticCurveTo(x + width, y, x + width, y + radius);
this.lineTo(x + width, y + height - radius);
this.quadraticCurveTo(x + width, y + height, x + width - radius, y+ height);
this.lineTo(x + radius, y + height);
this.quadraticCurveTo(x, y + height, x, y + height - radius);
this.lineTo(x, y + radius);
this.quadraticCurveTo(x, y, x + radius, y);
this.closePath();
if (stroke) {
this.stroke();
}
if (fill) {
this.fill();
}
};
CanvasRenderingContext2D.prototype.dashedLineTo = function (fromX, fromY, toX, toY, pattern) {
// default interval distance -> 5px
if (typeof pattern === "undefined") {
pattern = 5;
}
// calculate the delta x and delta y
var dx = (toX - fromX);
var dy = (toY - fromY);
var distance = Math.floor(Math.sqrt(dx*dx + dy*dy));
var dashlineInteveral = (pattern <= 0) ? distance : (distance/pattern);
var deltay = (dy/distance) * pattern;
var deltax = (dx/distance) * pattern;
// draw dash line
this.beginPath();
for(var dl=0; dl<dashlineInteveral; dl++) {
if(dl%2) {
this.lineTo(fromX + dl*deltax, fromY + dl*deltay);
} else {
this.moveTo(fromX + dl*deltax, fromY + dl*deltay);
}
}
this.stroke();
};

HTML中調(diào)用演示:

復(fù)制代碼
代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="chrome=IE8">
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Canvas Rounded Rectangle Demo</title>
<script src="fishcomponent.js"></script>
<link href="default.css" rel="stylesheet" />
<script>
var ctx = null; // global variable 2d context
var imageTexture = null;
window.onload = function() {
var canvas = document.getElementById("text_canvas");
console.log(canvas.parentNode.clientWidth);
canvas.width = canvas.parentNode.clientWidth;
canvas.height = canvas.parentNode.clientHeight;
if (!canvas.getContext) {
console.log("Canvas not supported. Please install a HTML5 compatible browser.");
return;
}
var context = canvas.getContext('2d');
context.strokeStyle="red";
context.fillStyle="RGBA(100,255,100, 0.5)";
context.roundRect(50, 50, 150, 150, 5, true);
context.strokeStyle="blue";
for(var i=0; i<10; i++) {
var delta = i*20;
var pattern = i*2+1;
context.dashedLineTo(250, 50+delta, 550, 50+delta, pattern);
}
}
</script>
</head>
<body>
<h1>HTML5 Canvas Dash-line Demo - By Gloomy Fish</h1>
<pre>Dash line and Rounded Rectangle</pre>
<div id="box_plot">
<canvas id="text_canvas"></canvas>
</div>
</body>
</html>

標(biāo)簽:黃石 公主嶺 南平 平頂山 黃石 陜西 山南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《HTML5 Canvas自定義圓角矩形與虛線示例代碼》,本文關(guān)鍵詞  HTML5,Canvas,自定義,圓角,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《HTML5 Canvas自定義圓角矩形與虛線示例代碼》相關(guān)的同類信息!
  • 本頁收集關(guān)于HTML5 Canvas自定義圓角矩形與虛線示例代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章