HTML5 Canvas自定义圆角矩形与虚线示例代码

2016-02-19 10:30 33 1 收藏

下面是个HTML5 Canvas自定义圆角矩形与虚线示例代码教程,撑握了其技术要点,学起来就简单多了。赶紧跟着图老师小编一起来看看吧!

【 tulaoshi.com - Web开发 】

HTML5 Canvas自定义圆角矩形与虚线(RoundedRectangle and Dash Line)

实现向HTML Canvas 2d context绘制对象中添加自定义的函数功能演示,如何绘制虚线以及控制虚线间隔大小,学会绘制圆角矩形的技巧。

HTML5 Canvas绘制对象中提供的原生功能没有实现绘制圆角矩形与虚线的功能,但是通过JavaScript语言的Object.prototype可以实现对对象CanvasRenderingContext2D添加这两个函数功能。代码的演示效果如下:
 
组件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; dldashlineInteveral; dl++) {
if(dl%2) {
this.lineTo(fromX + dl*deltax, fromY + dl*deltay);
} else {
this.moveTo(fromX + dl*deltax, fromY + dl*deltay);
}
}
this.stroke();
};

HTML中调用演示:

代码如下:

!DOCTYPE html
html
head
meta http-equiv="X-UA-Compatible" content="chrome=IE8"
meta http-equiv="Content-type" content="text/html;charset=UTF-8"
titleCanvas 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; i10; i++) {
var delta = i*20;
var pattern = i*2+1;
context.dashedLineTo(250, 50+delta, 550, 50+delta, pattern);
}
}
/script
/head
body
h1HTML5 Canvas Dash-line Demo - By Gloomy Fish/h1
preDash line and Rounded Rectangle/pre
div id="box_plot"
canvas id="text_canvas"/canvas
/div
/body
/html

来源:https://www.tulaoshi.com/n/20160219/1594878.html

延伸阅读
标签: Web开发
核心代码: 复制代码代码如下: $(function(){ var can = $("#can").get(0); var txi = can.getContext("2d"); txi.beginPath(); txi.moveTo(105,105); txi.lineTo(105,45); txi.lineTo(45,105); txi.closePath(); txi.stroke(); txi.beginPath(); txi.moveTo(25,25); txi.lineTo(100,25); txi.lineTo(25,100); txi.stroke(); })...
标签: Web开发
演示地址: HTML5拍照演示 首先,我们看看HTML代码结构,当然,这部分的DOM内容应该是在用户允许使用其摄像头事件出发后,动态加载生成的。 注意: 我们采用的是 640X480的分辨率,如果采用JS动态生成,那么您是可以灵活控制分辨率的。 代码如下: !-- 声明: 此div应该在允许使用webcam,网络摄像头之后动态生成 宽高: 640 *480,当然,...
标签: Web开发
HTML5 Canvas中提供了实现图形平移,旋转,放缩的API。 平移(translate) 平移坐标translate(x, y)意思是把(0,0)坐标平移到(x, y),原来的(0,0)坐标则变成(-x, -y) 图示如下:   任何原来的坐标点p(ox, oy)在translate之后的坐标点为p(ox-x, oy-y),其中点(x, y)为平移 点坐标translate(x, y)。 代码演示: 代码如下: // trans...
标签: Web开发
效果图: 注:下面的代码运行效果,请在支持html5浏览下执行,才能看到效果。 !doctype html html head titlecanvas简单画板/title style type="text/css" #can{ width:600px; height:500px; border:1px solid #ccc; margin-top:0px; margin-left:20px;} /style /head body h2 style="padding-left:20px"canvas简单画板/h2 canvas id="can...
标签: Web开发
这篇将要介绍一下 Canvas 中的基本图形。 图形的基础 - 路径 在 Canvas 中,所有基本图形都是以路径为基础的,也就是说,我们在调用 2dContext 的 lineTo、rect 等方法时,其实就是往已经的 context 路径集合中再添加一些路径点,在最后使用 fill 或 stroke&nbs...

经验教程

298

收藏

47
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部