把svg图片生成到svg

While simple circles and paths are easy enough to generate by hand-coding or a vector application, complex illustrations with regular geometry are often easiest to generate with some programming. A good example of the latter is a compass rose, shown above, which was mostly created using JavaScript.

虽然简单的圆和路径很容易通过手工编码或矢量应用程序生成,但是具有规则几何形状的复杂插图通常最容易通过某些编程生成。 后者的一个很好的例子是上面显示的罗盘玫瑰,它主要是使用JavaScript创建的。

标记 (The Markup)

I started the design on the page with a SVG element that contains only symbols:

我从包含符号的SVG元素开始页面设计:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 500 500" id="compassrose">
<defs>  <symbol><line x1="40" y1="250" x2="50" y2="250" id="roseline" /><line x1="40" y1="250" x2="60" y2="250" id="majline" /><path d="M10,250a240,240 0 1,0 480,0a240,240 0 1,0 -480,0" id="rosecircle" /></symbol></defs>
</svg>

Because <symbol> elements do not render by themselves, the SVG won’t be “seen” until it is completed with JavaScript. The <path> will be used as a guide to place elements on a circle, including the short and long degree lines (roseline and majline, respectively).

因为<symbol>元素不是自己呈现的,所以只有在用JavaScript完成后,才能“看到” SVG。 <path>将用作在圆上放置元素的指南,包括短度和长度线(分别为roselinemajline )。

CSS (The CSS)

The SVG is styled so that it is in the center of the page, and the lines and text provided with an appearance with CSS:

设置了SVG的样式,使其位于页面的中心,并且CSS提供了带有外观的行和文本:

#compassrose { width: 40%;border: 1px solid rgba(255,255,255,0.3);margin-left: 30%;border-radius: 50%;
}
#roseline, #majline {stroke: #eee;stroke-width: .5;
}
#compassrose text {font-family: Montserrat, sans-serif;font-size: 10;fill: #eee;
}

Note the slightly unusual appearance of the svg element, which is given a border-radius to make it appear as the outside of the compass: the SVG element itself can be made to look circular.

请注意svg元素的外观略有不同寻常,该元素具有一个border-radius ,使其看起来像罗盘的外部:SVG元素本身可以看起来是圆形的。

创建线 (Creating the Lines)

The script to make the compass lines is added to the end of the document:

用于制作指南针线的脚本已添加到文档的末尾:

var lineInc = 2,
majMarkDegree = 10,
degreeInc = 30,
compassrose = document.getElementById("compassrose"),
xmlns = "http://www.w3.org/2000/svg",
xlink = "http://www.w3.org/1999/xlink";
if (lineInc > 0) {for (var i=0; i < 360; i+=lineInc) {var newline = document.createElementNS(xmlns,'use'); if (i % majMarkDegree == 0) {           newline.setAttributeNS(xlink,'xlink:href','#majline');} else {newline.setAttributeNS(xlink,'xlink:href','#roseline');}
newline.setAttributeNS(null,'transform','rotate('+i+' 250 250)');
compassrose.appendChild(newline);
}

The variables are:

变量是:

  • lineInc: how many degrees apart the markings are

    lineInc :标记相隔多少度

  • majMarkDegree: how many degrees apart the major markings are

    majMarkDegree :主要标记相隔多少度

  • degreeInc: the numerical separation between the degrees printed around the edge of the circle

    degreeInc :围绕圆边打印的度数之间的数值间隔

The for loop increments by the amount specified by lineInc. At every increment, a <use> element is created. If the incremented amount is divisible by majMarkDegree via a modulus operator, then the majline is used; otherwise, roseline is added instead. Each line is rotated into the orientation provided by i.

for循环以lineInc指定的量递增。 每次增加一个<use>元素。 如果增加的量可以通过majMarkDegree通过模运算符整除,则使用majlinemajline将使用majline 。 否则,将添加roseline线。 每条线旋转到i提供的方向。

学位标记 (The Degree Markers)

The degree markers use startOffSet to position the text around the edge of the compass. startOffSet takes values from 0 to 100, so the loop is based on that.

度标记使用startOffSet将文本startOffSet在罗盘边缘周围。 startOffSet的取值范围是0100 ,因此循环基于该值。

Above 0 - 9 degrees, the printed numeral will be slightly out of alignment on the circle, since the text starts at the degree point. I’ve used a somewhat unusual equation with log to determine how many numerals are in the number, and (if it is longer than a single digit), the script pulls the rotation of the number back by a degree:

高于0-9度时,由于文本始于度点,因此打印的数字将略微偏离圆的对齐方式。 我在log使用了一个有点不寻常的方程式来确定数字中有多少个数字,并且(如果它长于一个数字),脚本会将数字的旋转拉回一个角度:

var writeDegs = document.createElementNS(xmlns,'text'),
currentDeg = 0,
writeOffset = 0;
for (var i=0; i < 99; i+=(degreeInc/360)*100) {var degree = document.createElementNS(xmlns,'textPath');degree.setAttributeNS(xlink,'xlink:href','#rosecircle');var length = Math.log(i) * Math.LOG10E + 1 | 0;if (length > 1) { writeOffset = 1; } degree.setAttributeNS(null,'startOffset',(i - writeOffset)+"%");degree.textContent = currentDeg;writeDegs.appendChild(degree);currentDeg += degreeInc;
}
compassrose.appendChild(writeDegs);

This isn’t perfectly accurate mathematically (to achieve that would require a bit more JavaScript) but it’s close enough for our purposes.

这在数学上并不是完全准确的(要实现这一点需要更多JavaScript),但对于我们的目的来说已经足够接近了。

动画 (The Animation)

I’ve also animated the compass to sway back and forth using the Web Animation API, using a similar technique to my “Random Walk” article. (Note that this animation will only work in Chrome and Firefox, at least currently).

我还使用与我的“ Random Walk”文章类似的技术,使用Web Animation API制作了指南针来回摆动的动画。 (请注意,此动画至少在Chrome和Firefox中才有效)。

function randomRot() {var oldOrientation = newOrientation;newOrientation =  Math.floor(Math.random() * 240);compassrose.animate([{ transform: 'rotate('+ oldOrientation+'deg)' },{ transform: 'rotate('+ newOrientation+'deg)' }], {duration: Math.abs(oldOrientation - newOrientation) * 30,fill: 'forwards'}).onfinish = function() {randomRot();}
}
newOrientation = 0;
randomRot();

The function compares the oldOrientation of the compass with the newOrientation of the element (a random number between 0 and 240, interpreted as degrees) and animates between them, with a duration calculated as the difference between the orientations multiplied by 30 (interpreted as time in milliseconds).

的函数的比较oldOrientation与罗盘的newOrientation的元件的(0和240之间的随机数,解释为度)和它们之间的动画,用(如取向乘以30之间的差计算的持续时间在解释为时间毫秒)。

结论 (Conclusion)

There are many other ways to create SVG with JavaScript, which I’ll go into more depth in future articles; for now, I hope this might prove a useful starting point for your own experiments.

还有很多其他用JavaScript创建SVG的方法,我将在以后的文章中更深入地介绍。 目前,我希望这可以为您自己的实验提供一个有用的起点。

翻译自: https://thenewcode.com/524/Lost-An-SVG-Compass-Rose-Generator

把svg图片生成到svg

把svg图片生成到svg_“迷失”:SVG指南针玫瑰生成器相关推荐

  1. html svg图片不显示,html/css svg怎么显示不出来?

    html/css svg怎么显示不出来?在html中引入svg文件为何不显示?下面给大家介绍一下原因和解决方案.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. 当html引入svg文 ...

  2. 【Android 安装包优化】Android 中使用 SVG 图片 ( SVG 矢量图简介 | Android 中生成 Vector 矢量图资源 )

    文章目录 一.SVG 矢量图简介 二.Android 中生成 Vector 矢量图资源 三.参考资料 一.SVG 矢量图简介 Android SVG 参考文档 : https://developer. ...

  3. 动态修改svg图片颜色

    使用场景 引入颜色为纯色的svg图片时,动态修改svg图片填充颜色.引入的方式包括直接svg代码引用和img标签间接引用. 直接引用SVG 如果通过svg代码的方式引入图片,那么可以直接修改fill属 ...

  4. android支持svg格式图片么,Android Studio2.0中使用SVG图片格式

    SVG格式, 适应屏幕, 图片较小, 还有很多优点 . 本文讲解如何使用SVG格式. SVG: Scalable Vector Graphics, 可缩放矢量图形. IRI: Internationa ...

  5. svg图片在vue项目中的应用

    svg图片在vue项目中的应用 一.安装 svg-sprite-loader 插件 npm install svg-sprite-loader 二.基于 vue-cli2.x 项目 webpack 配 ...

  6. 如何让电脑显示SVG图片的缩略图

    如何让电脑显示SVG图片的缩略图 工具/原料 SVG图形文件的缩略图补丁程序 方法/步骤 1 默认电脑是无法查看SVG图形文件的缩略图. 2 安装SVG图形文件的缩略图补丁,运行dssee_setup ...

  7. CAD图纸如何转换成白色背景的SVG图片

    在CAD制图的时候,常常 需要转换CAD图纸格式,在这其中就有CAD图纸转SVG图片格式.SVG是常用的图像格式之一,属于可缩放矢量图形.优点是可以让用户直接用代码来描绘图像,而且用任 何文字处理工具 ...

  8. 05 react img css修改svg图片样式

    react img css修改svg图片样式 svg图片的相关理论 定义 优点 前端引入svg图片的方式 方式一:<svg>标签引入,内嵌到 HTML 中 方式二,修改svg的颜色 方式三 ...

  9. 01.使用.svg格式图片生成app图标详细步骤和注意事项

    Android8.0之后引入了矢量图标,可以支持动态特效,在不同安卓版本上可以相对应的显示圆形.方形圆角,方形图标 需要两张.svg格式的图片,一张前景图,一张背景图,分别命名为ic_launcher ...

最新文章

  1. Jürgen Schmidhuber回顾30年前旧作,称其启发了现今流行的很多概念
  2. 创新实训团队记录:为BR-MTC问题设计一个近似算法
  3. java三角形剪角_大班数学:拼角剪角
  4. Flume整合Kafka采集滚动的日志
  5. python基本运算符_06-Python基础知识学习---基本运算符
  6. 盘点世界上最奇怪的6种编程语言
  7. 兼容IE和FF:获取Referer的JS和PHP方法 及 PHP利用curl伪造IP和来路
  8. 04 | 事件调度层:为什么 EventLoop 是 Netty 的精髓?
  9. yy主播旁边的机器人_YY上三位“聪明”主播,伽柏垄断主播设备,其余两位成为老板...
  10. 回归分析什么时候取对数_为什么相关或回归分析时 x和y取log
  11. Python数据可视化交互基本
  12. 华为云为基因检测保驾护航,助力健康行业发展
  13. “真功夫”与“花拳绣腿”
  14. ATMel的AT89C52芯片慎选
  15. java计算机毕业设计个人交友网站源程序+mysql+系统+lw文档+远程调试
  16. ppt中加入html,如何在ppt中插入html网页.ppt
  17. python module docs是什么意思_Python Module和Package辨析
  18. 什么是wms仓储管理系统?
  19. Intel CPU集成显卡被UEFI BIOS禁用想开启的设置
  20. 基于单片机的波形发生器设计

热门文章

  1. cmd输入cl提示不是内部命令
  2. 白魔法师-牛客小白月赛25
  3. 网络游戏运营的整体流程
  4. 物理学转行?读了四年物理学,优势在哪里?
  5. 使用postman interceptor拦截浏览器和手机请求
  6. YGG SEA 投资 5 万美元,购买 ARPG 游戏《变形机甲》中的 NFT 资产
  7. Velo项目评级:BB,展望稳定 | TokenInsight
  8. IOS 之FishHook原理及例子
  9. pathway 中几张特殊的通路图
  10. 嵌入式LINUX搭建arm环境,手把手教你嵌入式ARM开发环境搭建