HTML5 Canvas自定義圓角矩形與虛線示例代碼
來源:易賢網(wǎng) 閱讀:1295 次 日期:2016-07-13 13:46:27
溫馨提示:易賢網(wǎng)小編為您整理了“HTML5 Canvas自定義圓角矩形與虛線示例代碼”,方便廣大網(wǎng)友查閱!

HTML5 Canvas繪制對象中提供的原生功能沒有實(shí)現(xiàn)繪制圓角矩形與虛線的功能,通過JavaScript的Object.prototype可以實(shí)現(xiàn)對對象CanvasRenderingContext2D添加這兩個(gè)函數(shù)功能,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下

組件fishcomponent.js的代碼如下:

代碼如下:

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)用演示:

代碼如下:

<!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>

更多信息請查看網(wǎng)頁制作
易賢網(wǎng)手機(jī)網(wǎng)站地址:HTML5 Canvas自定義圓角矩形與虛線示例代碼
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)