在Html5里面的Canvas2D,如何画虚线呢? 如题,似乎接口都是画实线的,虚线,点画线怎么画呢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 确实,找了很久,CSS是可以,可是这两个搞在一起又比较麻烦,毕竟理念不同。总不能我一小段画,一小段不画吧 当时找的一个C++的画虚线函数自己改的,这里面直接扩展了CanvasRenderingContext2D对象,也就是说你getContent('2d')返回的对象可以直接使用。var ctx = window.CanvasRenderingContext2D && CanvasRenderingContext2D.prototype; if (ctx && ctx.lineTo){ ctx.dottedLine = dottedLine; } else { window.dottedLine = dottedLine; } function dottedLine(x, y, x2, y2, dotArray, ctx){ var context = ctx || this, dashCount = dotArray.length, dx = (x2 - x), dy = (y2 - y), xSlope = dx > dy, slope = (xSlope) ? dy / dx : dx / dy; if(slope > 9999) { slope = 9999; } else if(slope < -9999) { slope = -9999; } var distRemaining = Math.sqrt(dx * dx + dy * dy); var dashIndex = 0, draw = true; while(distRemaining >= 0.1 && dashIndex < 10000) { var dashLength = dotArray[dashIndex++ % dashCount]; if(dashLength === 0) { dashLength = 0.001; } if(dashLength > distRemaining) { dashLength = distRemaining; } var step = Math.sqrt(dashLength * dashLength / (1 + slope * slope)); if(xSlope) { x += dx < 0 && dy < 0 ? step * -1 : step; y += dx < 0 && dy < 0 ? slope * step * -1 : slope * step; } else { x += dx < 0 && dy < 0 ? slope * step * -1 : slope * step; y += dx < 0 && dy < 0 ? step * -1 : step; } context[draw ? 'lineTo' : 'moveTo'](x, y); distRemaining -= dashLength; draw = !draw; } context.moveTo(x2, y2); } 网页中调用控件以对话框最大显示,在下面的Jsp中做什么修改 js 怎么实现左键的下载功能 [^\\1] ... 如何使用js达到多个popup共存的效果 请看一下下段代码,究竟错在哪里!谢谢! 如何将特定文字插入到文本框的光标处 用了iframe怎么调iframe里的页面的方法? Javascript里显示类的实例名称的问题。 请教!获取json数据的方法 倒计时抽奖怎样保持每个用户看到的结果一致? dojo上传文件出错问题 求高手js 修改代码
总不能我一小段画,一小段不画吧
if (ctx && ctx.lineTo){
ctx.dottedLine = dottedLine;
} else {
window.dottedLine = dottedLine;
}
function dottedLine(x, y, x2, y2, dotArray, ctx){
var context = ctx || this,
dashCount = dotArray.length,
dx = (x2 - x), dy = (y2 - y),
xSlope = dx > dy,
slope = (xSlope) ? dy / dx : dx / dy;
if(slope > 9999) {
slope = 9999;
}
else if(slope < -9999) {
slope = -9999;
}
var distRemaining = Math.sqrt(dx * dx + dy * dy);
var dashIndex = 0, draw = true;
while(distRemaining >= 0.1 && dashIndex < 10000) {
var dashLength = dotArray[dashIndex++ % dashCount];
if(dashLength === 0) {
dashLength = 0.001;
}
if(dashLength > distRemaining) {
dashLength = distRemaining;
}
var step = Math.sqrt(dashLength * dashLength / (1 + slope * slope));
if(xSlope) {
x += dx < 0 && dy < 0 ? step * -1 : step;
y += dx < 0 && dy < 0 ? slope * step * -1 : slope * step;
}
else {
x += dx < 0 && dy < 0 ? slope * step * -1 : slope * step;
y += dx < 0 && dy < 0 ? step * -1 : step;
}
context[draw ? 'lineTo' : 'moveTo'](x, y);
distRemaining -= dashLength;
draw = !draw;
}
context.moveTo(x2, y2);
}