使用 SVG 和 Javascript 绘制 windBarbs
Draw windBarbs with SVG and Javascript
我是svg新手。我需要用jquery在svg中绘制风箭头。我在这里找到了一些使用 d3 绘制风箭头的代码。我将其转换为jquery兼容形式。这是jsfiddle示例。但它不起作用。谁能帮忙?
这是jquery代码:
$(function () {
var WindBarbArrowHandler = {
WindArrow: function (speed, direction, container, arrowWidth) {
'use strict';
var index = 0,
i;
this.speed = speed;
this.direction = direction;
this.trigDirection = direction + 90;
this.scale = arrowWidth / 8;
this.ten = 0;
this.five = 0;
this.fifty = 0;
// Create the canvas
$(container).append(
$('<svg></svg>')
.attr({ height: 2 * arrowWidth, width: 2 * arrowWidth })
);
$("svg", container).append('<defs></defs>');
$("defs", container).append($('<clipPath></clipPath>').attr('id', 'clip'));
$("clipPath", container).append($('<rect></rect>')
.attr({ height: 2 * arrowWidth, width: 2 * arrowWidth }));
// Draw the widget area
$("svg", container).append($('<g></g>').attr('class', 'wind-arrow'));
this.widget = $("svg", container);
if (this.speed > 0) {
// Prepare the path
this.path = "";
if (this.speed <= 7) {
// Draw a single line
this.longBar();
index = 1;
} else {
this.shortBar();
}
// Find the number of lines in function of the speed
this.five = Math.floor(this.speed / 5);
if (this.speed % 5 >= 3) {
this.five += 1;
}
// Add triangles (5 * 10)
this.fifty = Math.floor(this.five / 10);
this.five -= this.fifty * 10;
// Add tenLines (5 * 2)
this.ten = Math.floor(this.five / 2);
this.five -= this.ten * 2;
// Draw first the triangles
for (i = 0; i < this.fifty; i++) {
this.addFifty(index + 2 * i);
}
if (this.fifty > 0) {
index += 2 * (this.fifty - 0.5);
}
// Draw the long segments
for (i = 0; i < this.ten; i++) {
this.addTen(index + i);
}
index += this.ten;
// Draw the short segments
for (i = 0; i < this.five; i++) {
this.addFive(index + i);
}
this.path += "Z";
// Add to the widget
this.widget.append($('<g></g>'));
$("g", this.widget).append($('<path></path>').attr({
'd': this.path,
'vector-effect': 'non-scaling-stroke',
'transform': 'translate(' + arrowWidth + ', ' + arrowWidth + ') scale(' + this.scale + ') rotate(' + this.trigDirection + ' ' + 0 + ' ' + 0 + ') translate(-8, -2)',
'class': 'wind-arrow'
}));
}
},
shortBar: function () {
// Draw an horizontal short bar.
'use strict';
this.path += "M1 2 L8 2 ";
},
longBar: function () {
// Draw an horizontal long bar.
'use strict';
this.path += "M0 2 L8 2 ";
},
addTen: function (index) {
// Draw an oblique long segment corresponding to 10 kn.
'use strict';
this.path += "M" + index + " 0 L" + (index + 1) + " 2 ";
},
addFive: function (index) {
// Draw an oblique short segment corresponding to 10 kn.
'use strict';
this.path += "M" + (index + 0.5) + " 1 L" + (index + 1) + " 2 ";
},
addFifty: function (index) {
// Draw a triangle corresponding to 50 kn.
'use strict';
this.path += "M" + index + " 0 L" + (index + 1) + " 2 L" + index + " 2 L" + index + " 0 ";
},
};
WindBarbArrowHandler.WindArrow(30,45, $("#windBarbArrow"), 40);
});
HTML 代码如下所示:
<div id="windBarbArrow"></div>
只要使用d3
,你永远不会回头。
如果你对jQuery有一心之心,那么有一个烦人的陷阱。
它归结为代码中动态创建 SVG 元素的任何位置:
$('<g></g>')
改为:
$(document.createElementNS('http://www.w3.org/2000/svg', 'g'))
这是您的代码已更新并正常工作。
相关文章:
- D3在一个调用中绘制不同的SVG形状,没有可见性
- 有没有一个javascript图形绘制库可以进行气球树布局
- 使用相同的数据集绘制各种符号
- 使用onclick绘制SVG路径
- 在谷歌地图上绘制位置数据库
- 用chart.js绘制条形图
- “可绘制地图”设置地图选项“纬度-经度”
- 多维数据集网格未在指定的分区中绘制
- 使用谷歌图表在x轴上绘制日期
- 使用fabric.js从矩形区域获取对象,并将该区域绘制到画布上
- 如何在React Native中绘制图形
- 画布中绘制的矩形区域的弹出工具提示
- 如何在OpenLayers中获取动态绘制的多边形的坐标
- 画布:逐像素绘制图像并请求动画帧计时
- D3从嵌套的JSON中绘制第二个圆环图
- 如何用d3.js绘制折线图
- 在WebGL中绘制多个二维图像
- JSXGraph:如何绘制带有箭头和无点标记的矢量
- 用图像而不是颜色填充对象(将图像绘制到画布上)
- 使用 SVG 和 Javascript 绘制 windBarbs