canvas2svg.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  1. /*!!
  2. * Canvas 2 Svg v1.0.19
  3. * A low level canvas to SVG converter. Uses a mock canvas context to build an SVG document.
  4. *
  5. * Licensed under the MIT license:
  6. * http://www.opensource.org/licenses/mit-license.php
  7. *
  8. * Author:
  9. * Kerry Liu
  10. *
  11. * Copyright (c) 2014 Gliffy Inc.
  12. */
  13. (function() {
  14. 'use strict';
  15. var STYLES, ctx, CanvasGradient, CanvasPattern, namedEntities;
  16. //helper function to format a string
  17. function format(str, args) {
  18. var keys = Object.keys(args),
  19. i;
  20. for (i = 0; i < keys.length; i++) {
  21. str = str.replace(
  22. new RegExp('\\{' + keys[i] + '\\}', 'gi'),
  23. args[keys[i]]
  24. );
  25. }
  26. return str;
  27. }
  28. //helper function that generates a random string
  29. function randomString(holder) {
  30. var chars, randomstring, i;
  31. if (!holder) {
  32. throw new Error(
  33. 'cannot create a random attribute name for an undefined object'
  34. );
  35. }
  36. chars = 'ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';
  37. randomstring = '';
  38. do {
  39. randomstring = '';
  40. for (i = 0; i < 12; i++) {
  41. randomstring += chars[Math.floor(Math.random() * chars.length)];
  42. }
  43. } while (holder[randomstring]);
  44. return randomstring;
  45. }
  46. //helper function to map named to numbered entities
  47. function createNamedToNumberedLookup(items, radix) {
  48. var i,
  49. entity,
  50. lookup = {},
  51. base10,
  52. base16;
  53. items = items.split(',');
  54. radix = radix || 10;
  55. // Map from named to numbered entities.
  56. for (i = 0; i < items.length; i += 2) {
  57. entity = '&' + items[i + 1] + ';';
  58. base10 = parseInt(items[i], radix);
  59. lookup[entity] = '&#' + base10 + ';';
  60. }
  61. //FF and IE need to create a regex from hex values ie &nbsp; == \xa0
  62. lookup['\\xa0'] = '&#160;';
  63. return lookup;
  64. }
  65. //helper function to map canvas-textAlign to svg-textAnchor
  66. function getTextAnchor(textAlign) {
  67. //TODO: support rtl languages
  68. var mapping = {
  69. left: 'start',
  70. right: 'end',
  71. center: 'middle',
  72. start: 'start',
  73. end: 'end'
  74. };
  75. return mapping[textAlign] || mapping.start;
  76. }
  77. //helper function to map canvas-textBaseline to svg-dominantBaseline
  78. function getDominantBaseline(textBaseline) {
  79. //INFO: not supported in all browsers
  80. var mapping = {
  81. alphabetic: 'alphabetic',
  82. hanging: 'hanging',
  83. top: 'text-before-edge',
  84. bottom: 'text-after-edge',
  85. middle: 'central'
  86. };
  87. return mapping[textBaseline] || mapping.alphabetic;
  88. }
  89. // Unpack entities lookup where the numbers are in radix 32 to reduce the size
  90. // entity mapping courtesy of tinymce
  91. namedEntities = createNamedToNumberedLookup(
  92. '50,nbsp,51,iexcl,52,cent,53,pound,54,curren,55,yen,56,brvbar,57,sect,58,uml,59,copy,' +
  93. '5a,ordf,5b,laquo,5c,not,5d,shy,5e,reg,5f,macr,5g,deg,5h,plusmn,5i,sup2,5j,sup3,5k,acute,' +
  94. '5l,micro,5m,para,5n,middot,5o,cedil,5p,sup1,5q,ordm,5r,raquo,5s,frac14,5t,frac12,5u,frac34,' +
  95. '5v,iquest,60,Agrave,61,Aacute,62,Acirc,63,Atilde,64,Auml,65,Aring,66,AElig,67,Ccedil,' +
  96. '68,Egrave,69,Eacute,6a,Ecirc,6b,Euml,6c,Igrave,6d,Iacute,6e,Icirc,6f,Iuml,6g,ETH,6h,Ntilde,' +
  97. '6i,Ograve,6j,Oacute,6k,Ocirc,6l,Otilde,6m,Ouml,6n,times,6o,Oslash,6p,Ugrave,6q,Uacute,' +
  98. '6r,Ucirc,6s,Uuml,6t,Yacute,6u,THORN,6v,szlig,70,agrave,71,aacute,72,acirc,73,atilde,74,auml,' +
  99. '75,aring,76,aelig,77,ccedil,78,egrave,79,eacute,7a,ecirc,7b,euml,7c,igrave,7d,iacute,7e,icirc,' +
  100. '7f,iuml,7g,eth,7h,ntilde,7i,ograve,7j,oacute,7k,ocirc,7l,otilde,7m,ouml,7n,divide,7o,oslash,' +
  101. '7p,ugrave,7q,uacute,7r,ucirc,7s,uuml,7t,yacute,7u,thorn,7v,yuml,ci,fnof,sh,Alpha,si,Beta,' +
  102. 'sj,Gamma,sk,Delta,sl,Epsilon,sm,Zeta,sn,Eta,so,Theta,sp,Iota,sq,Kappa,sr,Lambda,ss,Mu,' +
  103. 'st,Nu,su,Xi,sv,Omicron,t0,Pi,t1,Rho,t3,Sigma,t4,Tau,t5,Upsilon,t6,Phi,t7,Chi,t8,Psi,' +
  104. 't9,Omega,th,alpha,ti,beta,tj,gamma,tk,delta,tl,epsilon,tm,zeta,tn,eta,to,theta,tp,iota,' +
  105. 'tq,kappa,tr,lambda,ts,mu,tt,nu,tu,xi,tv,omicron,u0,pi,u1,rho,u2,sigmaf,u3,sigma,u4,tau,' +
  106. 'u5,upsilon,u6,phi,u7,chi,u8,psi,u9,omega,uh,thetasym,ui,upsih,um,piv,812,bull,816,hellip,' +
  107. '81i,prime,81j,Prime,81u,oline,824,frasl,88o,weierp,88h,image,88s,real,892,trade,89l,alefsym,' +
  108. '8cg,larr,8ch,uarr,8ci,rarr,8cj,darr,8ck,harr,8dl,crarr,8eg,lArr,8eh,uArr,8ei,rArr,8ej,dArr,' +
  109. '8ek,hArr,8g0,forall,8g2,part,8g3,exist,8g5,empty,8g7,nabla,8g8,isin,8g9,notin,8gb,ni,8gf,prod,' +
  110. '8gh,sum,8gi,minus,8gn,lowast,8gq,radic,8gt,prop,8gu,infin,8h0,ang,8h7,and,8h8,or,8h9,cap,8ha,cup,' +
  111. '8hb,int,8hk,there4,8hs,sim,8i5,cong,8i8,asymp,8j0,ne,8j1,equiv,8j4,le,8j5,ge,8k2,sub,8k3,sup,8k4,' +
  112. 'nsub,8k6,sube,8k7,supe,8kl,oplus,8kn,otimes,8l5,perp,8m5,sdot,8o8,lceil,8o9,rceil,8oa,lfloor,8ob,' +
  113. 'rfloor,8p9,lang,8pa,rang,9ea,loz,9j0,spades,9j3,clubs,9j5,hearts,9j6,diams,ai,OElig,aj,oelig,b0,' +
  114. 'Scaron,b1,scaron,bo,Yuml,m6,circ,ms,tilde,802,ensp,803,emsp,809,thinsp,80c,zwnj,80d,zwj,80e,lrm,' +
  115. '80f,rlm,80j,ndash,80k,mdash,80o,lsquo,80p,rsquo,80q,sbquo,80s,ldquo,80t,rdquo,80u,bdquo,810,dagger,' +
  116. '811,Dagger,81g,permil,81p,lsaquo,81q,rsaquo,85c,euro',
  117. 32
  118. );
  119. //Some basic mappings for attributes and default values.
  120. STYLES = {
  121. strokeStyle: {
  122. svgAttr: 'stroke', //corresponding svg attribute
  123. canvas: '#000000', //canvas default
  124. svg: 'none', //svg default
  125. apply: 'stroke' //apply on stroke() or fill()
  126. },
  127. fillStyle: {
  128. svgAttr: 'fill',
  129. canvas: '#000000',
  130. svg: null, //svg default is black, but we need to special case this to handle canvas stroke without fill
  131. apply: 'fill'
  132. },
  133. lineCap: {
  134. svgAttr: 'stroke-linecap',
  135. canvas: 'butt',
  136. svg: 'butt',
  137. apply: 'stroke'
  138. },
  139. lineJoin: {
  140. svgAttr: 'stroke-linejoin',
  141. canvas: 'miter',
  142. svg: 'miter',
  143. apply: 'stroke'
  144. },
  145. miterLimit: {
  146. svgAttr: 'stroke-miterlimit',
  147. canvas: 10,
  148. svg: 4,
  149. apply: 'stroke'
  150. },
  151. lineWidth: {
  152. svgAttr: 'stroke-width',
  153. canvas: 1,
  154. svg: 1,
  155. apply: 'stroke'
  156. },
  157. globalAlpha: {
  158. svgAttr: 'opacity',
  159. canvas: 1,
  160. svg: 1,
  161. apply: 'fill stroke'
  162. },
  163. font: {
  164. //font converts to multiple svg attributes, there is custom logic for this
  165. canvas: '12px Arial'
  166. },
  167. shadowColor: {
  168. canvas: '#000000'
  169. },
  170. shadowOffsetX: {
  171. canvas: 0
  172. },
  173. shadowOffsetY: {
  174. canvas: 0
  175. },
  176. shadowBlur: {
  177. canvas: 0
  178. },
  179. textAlign: {
  180. canvas: 'start'
  181. },
  182. textBaseline: {
  183. canvas: 'alphabetic'
  184. },
  185. lineDash: {
  186. svgAttr: 'stroke-dasharray',
  187. canvas: [],
  188. svg: null,
  189. apply: 'stroke'
  190. }
  191. };
  192. /**
  193. *
  194. * @param gradientNode - reference to the gradient
  195. * @constructor
  196. */
  197. CanvasGradient = function(gradientNode, ctx) {
  198. this.__root = gradientNode;
  199. this.__ctx = ctx;
  200. };
  201. /**
  202. * Adds a color stop to the gradient root
  203. */
  204. CanvasGradient.prototype.addColorStop = function(offset, color) {
  205. var stop = this.__ctx.__createElement('stop'),
  206. regex,
  207. matches;
  208. stop.setAttribute('offset', offset);
  209. if (color.indexOf('rgba') !== -1) {
  210. //separate alpha value, since webkit can't handle it
  211. regex = /rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d?\.?\d*)\s*\)/gi;
  212. matches = regex.exec(color);
  213. stop.setAttribute(
  214. 'stop-color',
  215. format('rgb({r},{g},{b})', {
  216. r: matches[1],
  217. g: matches[2],
  218. b: matches[3]
  219. })
  220. );
  221. stop.setAttribute('stop-opacity', matches[4]);
  222. } else {
  223. stop.setAttribute('stop-color', color);
  224. }
  225. this.__root.appendChild(stop);
  226. };
  227. CanvasPattern = function(pattern, ctx) {
  228. this.__root = pattern;
  229. this.__ctx = ctx;
  230. };
  231. /**
  232. * The mock canvas context
  233. * @param o - options include:
  234. * ctx - existing Context2D to wrap around
  235. * width - width of your canvas (defaults to 500)
  236. * height - height of your canvas (defaults to 500)
  237. * enableMirroring - enables canvas mirroring (get image data) (defaults to false)
  238. * document - the document object (defaults to the current document)
  239. */
  240. ctx = function(o) {
  241. var defaultOptions = { width: 500, height: 500, enableMirroring: false },
  242. options;
  243. //keep support for this way of calling C2S: new C2S(width,height)
  244. if (arguments.length > 1) {
  245. options = defaultOptions;
  246. options.width = arguments[0];
  247. options.height = arguments[1];
  248. } else if (!o) {
  249. options = defaultOptions;
  250. } else {
  251. options = o;
  252. }
  253. if (!(this instanceof ctx)) {
  254. //did someone call this without new?
  255. return new ctx(options);
  256. }
  257. //setup options
  258. this.width = options.width || defaultOptions.width;
  259. this.height = options.height || defaultOptions.height;
  260. this.enableMirroring =
  261. options.enableMirroring !== undefined
  262. ? options.enableMirroring
  263. : defaultOptions.enableMirroring;
  264. this.canvas = this; ///point back to this instance!
  265. this.__document = options.document || document;
  266. // allow passing in an existing context to wrap around
  267. // if a context is passed in, we know a canvas already exist
  268. if (options.ctx) {
  269. this.__ctx = options.ctx;
  270. } else {
  271. this.__canvas = this.__document.createElement('canvas');
  272. this.__ctx = this.__canvas.getContext('2d');
  273. }
  274. this.__setDefaultStyles();
  275. this.__stack = [this.__getStyleState()];
  276. this.__groupStack = [];
  277. //the root svg element
  278. this.__root = this.__document.createElementNS(
  279. 'http://www.w3.org/2000/svg',
  280. 'svg'
  281. );
  282. this.__root.setAttribute('version', 1.1);
  283. this.__root.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
  284. this.__root.setAttributeNS(
  285. 'http://www.w3.org/2000/xmlns/',
  286. 'xmlns:xlink',
  287. 'http://www.w3.org/1999/xlink'
  288. );
  289. this.__root.setAttribute('width', this.width);
  290. this.__root.setAttribute('height', this.height);
  291. //make sure we don't generate the same ids in defs
  292. this.__ids = {};
  293. //defs tag
  294. this.__defs = this.__document.createElementNS(
  295. 'http://www.w3.org/2000/svg',
  296. 'defs'
  297. );
  298. this.__root.appendChild(this.__defs);
  299. //also add a group child. the svg element can't use the transform attribute
  300. this.__currentElement = this.__document.createElementNS(
  301. 'http://www.w3.org/2000/svg',
  302. 'g'
  303. );
  304. this.__root.appendChild(this.__currentElement);
  305. };
  306. /**
  307. * Creates the specified svg element
  308. * @private
  309. */
  310. ctx.prototype.__createElement = function(elementName, properties, resetFill) {
  311. if (typeof properties === 'undefined') {
  312. properties = {};
  313. }
  314. var element = this.__document.createElementNS(
  315. 'http://www.w3.org/2000/svg',
  316. elementName
  317. ),
  318. keys = Object.keys(properties),
  319. i,
  320. key;
  321. if (resetFill) {
  322. //if fill or stroke is not specified, the svg element should not display. By default SVG's fill is black.
  323. element.setAttribute('fill', 'none');
  324. element.setAttribute('stroke', 'none');
  325. }
  326. for (i = 0; i < keys.length; i++) {
  327. key = keys[i];
  328. element.setAttribute(key, properties[key]);
  329. }
  330. return element;
  331. };
  332. /**
  333. * Applies default canvas styles to the context
  334. * @private
  335. */
  336. ctx.prototype.__setDefaultStyles = function() {
  337. //default 2d canvas context properties see:http://www.w3.org/TR/2dcontext/
  338. var keys = Object.keys(STYLES),
  339. i,
  340. key;
  341. for (i = 0; i < keys.length; i++) {
  342. key = keys[i];
  343. this[key] = STYLES[key].canvas;
  344. }
  345. };
  346. ctx.prototype.setAttrs = function(pen) {
  347. if (!pen) {
  348. return;
  349. }
  350. var currentElement = this.__currentElement;
  351. currentElement.setAttribute('id', pen.id);
  352. currentElement.setAttribute('name', pen.name);
  353. pen.text && currentElement.setAttribute('text', pen.text);
  354. pen.lineName && currentElement.setAttribute('line-name', pen.lineName);
  355. // 连线起终点
  356. if (pen.type == 1) {
  357. const from = pen.anchors[0];
  358. from.connectTo && currentElement.setAttribute('from-id', from.connectTo);
  359. const to = pen.anchors[pen.anchors.length - 1];
  360. to.connectTo && currentElement.setAttribute('to-id', to.connectTo);
  361. }
  362. // 业务数据
  363. if (!Array.isArray(pen.form)) {
  364. return;
  365. }
  366. pen.form.forEach(({key}) => {
  367. // TODO: pen[key] 若是一个对象,待考虑
  368. currentElement.setAttribute('data-' + key, pen[key]);
  369. });
  370. };
  371. /**
  372. * Applies styles on restore
  373. * @param styleState
  374. * @private
  375. */
  376. ctx.prototype.__applyStyleState = function(styleState) {
  377. var keys = Object.keys(styleState),
  378. i,
  379. key;
  380. for (i = 0; i < keys.length; i++) {
  381. key = keys[i];
  382. this[key] = styleState[key];
  383. }
  384. };
  385. /**
  386. * Gets the current style state
  387. * @return {Object}
  388. * @private
  389. */
  390. ctx.prototype.__getStyleState = function() {
  391. var i,
  392. styleState = {},
  393. keys = Object.keys(STYLES),
  394. key;
  395. for (i = 0; i < keys.length; i++) {
  396. key = keys[i];
  397. styleState[key] = this[key];
  398. }
  399. return styleState;
  400. };
  401. /**
  402. * Apples the current styles to the current SVG element. On "ctx.fill" or "ctx.stroke"
  403. * @param type
  404. * @private
  405. */
  406. ctx.prototype.__applyStyleToCurrentElement = function(type) {
  407. var currentElement = this.__currentElement;
  408. var currentStyleGroup = this.__currentElementsToStyle;
  409. if (currentStyleGroup) {
  410. currentElement.setAttribute(type, '');
  411. currentElement = currentStyleGroup.element;
  412. currentStyleGroup.children.forEach(function(node) {
  413. node.setAttribute(type, '');
  414. });
  415. }
  416. var keys = Object.keys(STYLES),
  417. i,
  418. style,
  419. value,
  420. id,
  421. regex,
  422. matches;
  423. for (i = 0; i < keys.length; i++) {
  424. style = STYLES[keys[i]];
  425. value = this[keys[i]];
  426. if (style.apply) {
  427. //is this a gradient or pattern?
  428. if (value instanceof CanvasPattern) {
  429. //pattern
  430. if (value.__ctx) {
  431. //copy over defs
  432. while (value.__ctx.__defs.childNodes.length) {
  433. id = value.__ctx.__defs.childNodes[0].getAttribute('id');
  434. this.__ids[id] = id;
  435. this.__defs.appendChild(value.__ctx.__defs.childNodes[0]);
  436. }
  437. }
  438. currentElement.setAttribute(
  439. style.apply,
  440. format('url(#{id})', { id: value.__root.getAttribute('id') })
  441. );
  442. } else if (value instanceof CanvasGradient) {
  443. //gradient
  444. currentElement.setAttribute(
  445. style.apply,
  446. format('url(#{id})', { id: value.__root.getAttribute('id') })
  447. );
  448. } else if (style.apply.indexOf(type) !== -1 && style.svg !== value) {
  449. if (
  450. (style.svgAttr === 'stroke' || style.svgAttr === 'fill') &&
  451. value &&
  452. value.indexOf('rgba') !== -1
  453. ) {
  454. //separate alpha value, since illustrator can't handle it
  455. regex = /rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d?\.?\d*)\s*\)/gi;
  456. matches = regex.exec(value);
  457. currentElement.setAttribute(
  458. style.svgAttr,
  459. format('rgb({r},{g},{b})', {
  460. r: matches[1],
  461. g: matches[2],
  462. b: matches[3]
  463. })
  464. );
  465. //should take globalAlpha here
  466. var opacity = matches[4];
  467. var globalAlpha = this.globalAlpha;
  468. if (globalAlpha != null) {
  469. opacity *= globalAlpha;
  470. }
  471. currentElement.setAttribute(style.svgAttr + '-opacity', opacity);
  472. } else {
  473. var attr = style.svgAttr;
  474. if (keys[i] === 'globalAlpha') {
  475. attr = type + '-' + style.svgAttr;
  476. if (currentElement.getAttribute(attr)) {
  477. //fill-opacity or stroke-opacity has already been set by stroke or fill.
  478. continue;
  479. }
  480. }
  481. //otherwise only update attribute if right type, and not svg default
  482. currentElement.setAttribute(attr, value);
  483. }
  484. }
  485. }
  486. }
  487. };
  488. /**
  489. * Will return the closest group or svg node. May return the current element.
  490. * @private
  491. */
  492. ctx.prototype.__closestGroupOrSvg = function(node) {
  493. node = node || this.__currentElement;
  494. if (node.nodeName === 'g' || node.nodeName === 'svg') {
  495. return node;
  496. } else {
  497. return this.__closestGroupOrSvg(node.parentNode);
  498. }
  499. };
  500. /**
  501. * Returns the serialized value of the svg so far
  502. * @param fixNamedEntities - Standalone SVG doesn't support named entities, which document.createTextNode encodes.
  503. * If true, we attempt to find all named entities and encode it as a numeric entity.
  504. * @return serialized svg
  505. */
  506. ctx.prototype.getSerializedSvg = function(fixNamedEntities) {
  507. var serialized = new XMLSerializer().serializeToString(this.__root),
  508. keys,
  509. i,
  510. key,
  511. value,
  512. regexp,
  513. xmlns;
  514. //IE search for a duplicate xmnls because they didn't implement setAttributeNS correctly
  515. xmlns = /xmlns="http:\/\/www\.w3\.org\/2000\/svg".+xmlns="http:\/\/www\.w3\.org\/2000\/svg/gi;
  516. if (xmlns.test(serialized)) {
  517. serialized = serialized.replace(
  518. 'xmlns="http://www.w3.org/2000/svg',
  519. 'xmlns:xlink="http://www.w3.org/1999/xlink'
  520. );
  521. }
  522. if (fixNamedEntities) {
  523. keys = Object.keys(namedEntities);
  524. //loop over each named entity and replace with the proper equivalent.
  525. for (i = 0; i < keys.length; i++) {
  526. key = keys[i];
  527. value = namedEntities[key];
  528. regexp = new RegExp(key, 'gi');
  529. if (regexp.test(serialized)) {
  530. serialized = serialized.replace(regexp, value);
  531. }
  532. }
  533. }
  534. return serialized;
  535. };
  536. /**
  537. * Returns the root svg
  538. * @return
  539. */
  540. ctx.prototype.getSvg = function() {
  541. return this.__root;
  542. };
  543. /**
  544. * Will generate a group tag.
  545. */
  546. ctx.prototype.save = function() {
  547. var group = this.__createElement('g');
  548. var parent = this.__closestGroupOrSvg();
  549. this.__groupStack.push(parent);
  550. parent.appendChild(group);
  551. this.__currentElement = group;
  552. this.__stack.push(this.__getStyleState());
  553. };
  554. /**
  555. * Sets current element to parent, or just root if already root
  556. */
  557. ctx.prototype.restore = function() {
  558. this.__currentElement = this.__groupStack.pop();
  559. this.__currentElementsToStyle = null;
  560. //Clearing canvas will make the poped group invalid, currentElement is set to the root group node.
  561. if (!this.__currentElement) {
  562. this.__currentElement = this.__root.childNodes[1];
  563. }
  564. var state = this.__stack.pop();
  565. this.__applyStyleState(state);
  566. };
  567. /**
  568. * Helper method to add transform
  569. * @private
  570. */
  571. ctx.prototype.__addTransform = function(t) {
  572. //if the current element has siblings, add another group
  573. var parent = this.__closestGroupOrSvg();
  574. if (parent.childNodes.length > 0) {
  575. if (this.__currentElement.nodeName === 'path') {
  576. if (!this.__currentElementsToStyle)
  577. this.__currentElementsToStyle = { element: parent, children: [] };
  578. this.__currentElementsToStyle.children.push(this.__currentElement);
  579. this.__applyCurrentDefaultPath();
  580. }
  581. var group = this.__createElement('g');
  582. parent.appendChild(group);
  583. this.__currentElement = group;
  584. }
  585. var transform = this.__currentElement.getAttribute('transform');
  586. if (transform) {
  587. transform += ' ';
  588. } else {
  589. transform = '';
  590. }
  591. transform += t;
  592. this.__currentElement.setAttribute('transform', transform);
  593. };
  594. /**
  595. * scales the current element
  596. */
  597. ctx.prototype.scale = function(x, y) {
  598. if (y === undefined) {
  599. y = x;
  600. }
  601. this.__addTransform(format('scale({x},{y})', { x: x, y: y }));
  602. };
  603. /**
  604. * rotates the current element
  605. */
  606. ctx.prototype.rotate = function(angle) {
  607. var degrees = (angle * 180) / Math.PI;
  608. this.__addTransform(
  609. format('rotate({angle},{cx},{cy})', { angle: degrees, cx: 0, cy: 0 })
  610. );
  611. };
  612. /**
  613. * translates the current element
  614. */
  615. ctx.prototype.translate = function(x, y) {
  616. this.__addTransform(format('translate({x},{y})', { x: x, y: y }));
  617. };
  618. /**
  619. * applies a transform to the current element
  620. */
  621. ctx.prototype.transform = function(a, b, c, d, e, f) {
  622. this.__addTransform(
  623. format('matrix({a},{b},{c},{d},{e},{f})', {
  624. a: a,
  625. b: b,
  626. c: c,
  627. d: d,
  628. e: e,
  629. f: f
  630. })
  631. );
  632. };
  633. /**
  634. * Create a new Path Element
  635. */
  636. ctx.prototype.beginPath = function() {
  637. var path, parent;
  638. // Note that there is only one current default path, it is not part of the drawing state.
  639. // See also: https://html.spec.whatwg.org/multipage/scripting.html#current-default-path
  640. this.__currentDefaultPath = '';
  641. this.__currentPosition = {};
  642. path = this.__createElement('path', {}, true);
  643. parent = this.__closestGroupOrSvg();
  644. parent.appendChild(path);
  645. this.__currentElement = path;
  646. };
  647. /**
  648. * Helper function to apply currentDefaultPath to current path element
  649. * @private
  650. */
  651. ctx.prototype.__applyCurrentDefaultPath = function() {
  652. var currentElement = this.__currentElement;
  653. if (currentElement.nodeName === 'path') {
  654. currentElement.setAttribute('d', this.__currentDefaultPath);
  655. } else {
  656. console.error(
  657. 'Attempted to apply path command to node',
  658. currentElement.nodeName
  659. );
  660. }
  661. };
  662. ctx.prototype.svgPath = function(text) {
  663. this.__addPathCommand(text);
  664. };
  665. /**
  666. * Helper function to add path command
  667. * @private
  668. */
  669. ctx.prototype.__addPathCommand = function(command) {
  670. this.__currentDefaultPath += ' ';
  671. this.__currentDefaultPath += command;
  672. };
  673. /**
  674. * Adds the move command to the current path element,
  675. * if the currentPathElement is not empty create a new path element
  676. */
  677. ctx.prototype.moveTo = function(x, y) {
  678. if (this.__currentElement.nodeName !== 'path') {
  679. this.beginPath();
  680. }
  681. // creates a new subpath with the given point
  682. this.__currentPosition = { x: x, y: y };
  683. this.__addPathCommand(format('M {x} {y}', { x: x, y: y }));
  684. };
  685. /**
  686. * Closes the current path
  687. */
  688. ctx.prototype.closePath = function() {
  689. if (this.__currentDefaultPath) {
  690. this.__addPathCommand('Z');
  691. }
  692. };
  693. /**
  694. * Adds a line to command
  695. */
  696. ctx.prototype.lineTo = function(x, y) {
  697. this.__currentPosition = { x: x, y: y };
  698. if (this.__currentDefaultPath.indexOf('M') > -1) {
  699. this.__addPathCommand(format('L {x} {y}', { x: x, y: y }));
  700. } else {
  701. this.__addPathCommand(format('M {x} {y}', { x: x, y: y }));
  702. }
  703. };
  704. /**
  705. * Add a bezier command
  706. */
  707. ctx.prototype.bezierCurveTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
  708. this.__currentPosition = { x: x, y: y };
  709. this.__addPathCommand(
  710. format('C {cp1x} {cp1y} {cp2x} {cp2y} {x} {y}', {
  711. cp1x: cp1x,
  712. cp1y: cp1y,
  713. cp2x: cp2x,
  714. cp2y: cp2y,
  715. x: x,
  716. y: y
  717. })
  718. );
  719. };
  720. /**
  721. * Adds a quadratic curve to command
  722. */
  723. ctx.prototype.quadraticCurveTo = function(cpx, cpy, x, y) {
  724. this.__currentPosition = { x: x, y: y };
  725. this.__addPathCommand(
  726. format('Q {cpx} {cpy} {x} {y}', { cpx: cpx, cpy: cpy, x: x, y: y })
  727. );
  728. };
  729. /**
  730. * Return a new normalized vector of given vector
  731. */
  732. var normalize = function(vector) {
  733. var len = Math.sqrt(vector[0] * vector[0] + vector[1] * vector[1]);
  734. return [vector[0] / len, vector[1] / len];
  735. };
  736. /**
  737. * Adds the arcTo to the current path
  738. *
  739. * @see http://www.w3.org/TR/2015/WD-2dcontext-20150514/#dom-context-2d-arcto
  740. */
  741. ctx.prototype.arcTo = function(x1, y1, x2, y2, radius) {
  742. // Let the point (x0, y0) be the last point in the subpath.
  743. var x0 = this.__currentPosition && this.__currentPosition.x;
  744. var y0 = this.__currentPosition && this.__currentPosition.y;
  745. // First ensure there is a subpath for (x1, y1).
  746. if (typeof x0 == 'undefined' || typeof y0 == 'undefined') {
  747. return;
  748. }
  749. // Negative values for radius must cause the implementation to throw an IndexSizeError exception.
  750. if (radius < 0) {
  751. throw new Error(
  752. 'IndexSizeError: The radius provided (' + radius + ') is negative.'
  753. );
  754. }
  755. // If the point (x0, y0) is equal to the point (x1, y1),
  756. // or if the point (x1, y1) is equal to the point (x2, y2),
  757. // or if the radius radius is zero,
  758. // then the method must add the point (x1, y1) to the subpath,
  759. // and connect that point to the previous point (x0, y0) by a straight line.
  760. if ((x0 === x1 && y0 === y1) || (x1 === x2 && y1 === y2) || radius === 0) {
  761. this.lineTo(x1, y1);
  762. return;
  763. }
  764. // Otherwise, if the points (x0, y0), (x1, y1), and (x2, y2) all lie on a single straight line,
  765. // then the method must add the point (x1, y1) to the subpath,
  766. // and connect that point to the previous point (x0, y0) by a straight line.
  767. var unit_vec_p1_p0 = normalize([x0 - x1, y0 - y1]);
  768. var unit_vec_p1_p2 = normalize([x2 - x1, y2 - y1]);
  769. if (
  770. unit_vec_p1_p0[0] * unit_vec_p1_p2[1] ===
  771. unit_vec_p1_p0[1] * unit_vec_p1_p2[0]
  772. ) {
  773. this.lineTo(x1, y1);
  774. return;
  775. }
  776. // Otherwise, let The Arc be the shortest arc given by circumference of the circle that has radius radius,
  777. // and that has one point tangent to the half-infinite line that crosses the point (x0, y0) and ends at the point (x1, y1),
  778. // and that has a different point tangent to the half-infinite line that ends at the point (x1, y1), and crosses the point (x2, y2).
  779. // The points at which this circle touches these two lines are called the start and end tangent points respectively.
  780. // note that both vectors are unit vectors, so the length is 1
  781. var cos =
  782. unit_vec_p1_p0[0] * unit_vec_p1_p2[0] +
  783. unit_vec_p1_p0[1] * unit_vec_p1_p2[1];
  784. var theta = Math.acos(Math.abs(cos));
  785. // Calculate origin
  786. var unit_vec_p1_origin = normalize([
  787. unit_vec_p1_p0[0] + unit_vec_p1_p2[0],
  788. unit_vec_p1_p0[1] + unit_vec_p1_p2[1]
  789. ]);
  790. var len_p1_origin = radius / Math.sin(theta / 2);
  791. var x = x1 + len_p1_origin * unit_vec_p1_origin[0];
  792. var y = y1 + len_p1_origin * unit_vec_p1_origin[1];
  793. // Calculate start angle and end angle
  794. // rotate 90deg clockwise (note that y axis points to its down)
  795. var unit_vec_origin_start_tangent = [-unit_vec_p1_p0[1], unit_vec_p1_p0[0]];
  796. // rotate 90deg counter clockwise (note that y axis points to its down)
  797. var unit_vec_origin_end_tangent = [unit_vec_p1_p2[1], -unit_vec_p1_p2[0]];
  798. var getAngle = function(vector) {
  799. // get angle (clockwise) between vector and (1, 0)
  800. var x = vector[0];
  801. var y = vector[1];
  802. if (y >= 0) {
  803. // note that y axis points to its down
  804. return Math.acos(x);
  805. } else {
  806. return -Math.acos(x);
  807. }
  808. };
  809. var startAngle = getAngle(unit_vec_origin_start_tangent);
  810. var endAngle = getAngle(unit_vec_origin_end_tangent);
  811. // Connect the point (x0, y0) to the start tangent point by a straight line
  812. this.lineTo(
  813. x + unit_vec_origin_start_tangent[0] * radius,
  814. y + unit_vec_origin_start_tangent[1] * radius
  815. );
  816. // Connect the start tangent point to the end tangent point by arc
  817. // and adding the end tangent point to the subpath.
  818. this.arc(x, y, radius, startAngle, endAngle);
  819. };
  820. /**
  821. * Sets the stroke property on the current element
  822. */
  823. ctx.prototype.stroke = function() {
  824. if (this.__currentElement.nodeName === 'path') {
  825. this.__currentElement.setAttribute('paint-order', 'fill stroke markers');
  826. }
  827. this.__applyCurrentDefaultPath();
  828. this.__applyStyleToCurrentElement('stroke');
  829. };
  830. /**
  831. * Sets fill properties on the current element
  832. */
  833. ctx.prototype.fill = function() {
  834. if (this.__currentElement.nodeName === 'path') {
  835. this.__currentElement.setAttribute('paint-order', 'stroke fill markers');
  836. }
  837. this.__applyCurrentDefaultPath();
  838. this.__applyStyleToCurrentElement('fill');
  839. };
  840. /**
  841. * Adds a rectangle to the path.
  842. */
  843. ctx.prototype.rect = function(x, y, width, height) {
  844. if (this.__currentElement.nodeName !== 'path') {
  845. this.beginPath();
  846. }
  847. this.moveTo(x, y);
  848. this.lineTo(x + width, y);
  849. this.lineTo(x + width, y + height);
  850. this.lineTo(x, y + height);
  851. this.lineTo(x, y);
  852. this.closePath();
  853. };
  854. /**
  855. * adds a rectangle element
  856. */
  857. ctx.prototype.fillRect = function(x, y, width, height) {
  858. var rect, parent;
  859. rect = this.__createElement(
  860. 'rect',
  861. {
  862. x: x,
  863. y: y,
  864. width: width,
  865. height: height
  866. },
  867. true
  868. );
  869. parent = this.__closestGroupOrSvg();
  870. parent.appendChild(rect);
  871. this.__currentElement = rect;
  872. this.__applyStyleToCurrentElement('fill');
  873. };
  874. /**
  875. * Draws a rectangle with no fill
  876. * @param x
  877. * @param y
  878. * @param width
  879. * @param height
  880. */
  881. ctx.prototype.strokeRect = function(x, y, width, height) {
  882. var rect, parent;
  883. rect = this.__createElement(
  884. 'rect',
  885. {
  886. x: x,
  887. y: y,
  888. width: width,
  889. height: height
  890. },
  891. true
  892. );
  893. parent = this.__closestGroupOrSvg();
  894. parent.appendChild(rect);
  895. this.__currentElement = rect;
  896. this.__applyStyleToCurrentElement('stroke');
  897. };
  898. /**
  899. * Clear entire canvas:
  900. * 1. save current transforms
  901. * 2. remove all the childNodes of the root g element
  902. */
  903. ctx.prototype.__clearCanvas = function() {
  904. var current = this.__closestGroupOrSvg(),
  905. transform = current.getAttribute('transform');
  906. var rootGroup = this.__root.childNodes[1];
  907. var childNodes = rootGroup.childNodes;
  908. for (var i = childNodes.length - 1; i >= 0; i--) {
  909. if (childNodes[i]) {
  910. rootGroup.removeChild(childNodes[i]);
  911. }
  912. }
  913. this.__currentElement = rootGroup;
  914. //reset __groupStack as all the child group nodes are all removed.
  915. this.__groupStack = [];
  916. if (transform) {
  917. this.__addTransform(transform);
  918. }
  919. };
  920. /**
  921. * "Clears" a canvas by just drawing a white rectangle in the current group.
  922. */
  923. ctx.prototype.clearRect = function(x, y, width, height) {
  924. //clear entire canvas
  925. if (x === 0 && y === 0 && width === this.width && height === this.height) {
  926. this.__clearCanvas();
  927. return;
  928. }
  929. var rect,
  930. parent = this.__closestGroupOrSvg();
  931. rect = this.__createElement(
  932. 'rect',
  933. {
  934. x: x,
  935. y: y,
  936. width: width,
  937. height: height,
  938. fill: '#FFFFFF'
  939. },
  940. true
  941. );
  942. parent.appendChild(rect);
  943. };
  944. /**
  945. * Adds a linear gradient to a defs tag.
  946. * Returns a canvas gradient object that has a reference to it's parent def
  947. */
  948. ctx.prototype.createLinearGradient = function(x1, y1, x2, y2) {
  949. var grad = this.__createElement(
  950. 'linearGradient',
  951. {
  952. id: randomString(this.__ids),
  953. x1: x1 + 'px',
  954. x2: x2 + 'px',
  955. y1: y1 + 'px',
  956. y2: y2 + 'px',
  957. gradientUnits: 'userSpaceOnUse'
  958. },
  959. false
  960. );
  961. this.__defs.appendChild(grad);
  962. return new CanvasGradient(grad, this);
  963. };
  964. /**
  965. * Adds a radial gradient to a defs tag.
  966. * Returns a canvas gradient object that has a reference to it's parent def
  967. */
  968. ctx.prototype.createRadialGradient = function(x0, y0, r0, x1, y1, r1) {
  969. var grad = this.__createElement(
  970. 'radialGradient',
  971. {
  972. id: randomString(this.__ids),
  973. cx: x1 + 'px',
  974. cy: y1 + 'px',
  975. r: r1 + 'px',
  976. fx: x0 + 'px',
  977. fy: y0 + 'px',
  978. gradientUnits: 'userSpaceOnUse'
  979. },
  980. false
  981. );
  982. this.__defs.appendChild(grad);
  983. return new CanvasGradient(grad, this);
  984. };
  985. /**
  986. * Parses the font string and returns svg mapping
  987. * @private
  988. */
  989. ctx.prototype.__parseFont = function() {
  990. var fontPart = this.font.split(' ') || [];
  991. if (fontPart[3] && fontPart[3].indexOf('/') > -1) {
  992. fontPart[3] = fontPart[3].split('/')[0];
  993. }
  994. var family = '';
  995. for (var i = 4; i < fontPart.length; ++i) {
  996. family += fontPart[i] + ' ';
  997. }
  998. if (fontPart.length === 2) {
  999. family = fontPart[1];
  1000. fontPart = ['normal', 'normal', 'normal', fontPart[0]];
  1001. }
  1002. var data = {
  1003. style: fontPart[0] || 'normal',
  1004. size: fontPart[3] || '12px',
  1005. family: family || 'Arial',
  1006. weight: fontPart[2] || 'normal',
  1007. decoration: fontPart[1] || 'normal',
  1008. href: null
  1009. };
  1010. //canvas doesn't support underline natively, but we can pass this attribute
  1011. if (this.__fontUnderline === 'underline') {
  1012. data.decoration = 'underline';
  1013. }
  1014. //canvas also doesn't support linking, but we can pass this as well
  1015. if (this.__fontHref) {
  1016. data.href = this.__fontHref;
  1017. }
  1018. return data;
  1019. };
  1020. /**
  1021. * Helper to link text fragments
  1022. * @param font
  1023. * @param element
  1024. * @return {*}
  1025. * @private
  1026. */
  1027. ctx.prototype.__wrapTextLink = function(font, element) {
  1028. if (font.href) {
  1029. var a = this.__createElement('a');
  1030. a.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', font.href);
  1031. a.appendChild(element);
  1032. return a;
  1033. }
  1034. return element;
  1035. };
  1036. /**
  1037. * Fills or strokes text
  1038. * @param text
  1039. * @param x
  1040. * @param y
  1041. * @param action - stroke or fill
  1042. * @private
  1043. */
  1044. ctx.prototype.__applyText = function(text, x, y, action) {
  1045. var font = this.__parseFont(),
  1046. parent = this.__closestGroupOrSvg(),
  1047. textElement = this.__createElement(
  1048. 'text',
  1049. {
  1050. 'font-family': font.family,
  1051. 'font-size': font.size,
  1052. 'font-style': font.style,
  1053. 'font-weight': font.weight,
  1054. 'text-decoration': font.decoration,
  1055. x: x,
  1056. y: y,
  1057. 'text-anchor': getTextAnchor(this.textAlign),
  1058. 'dominant-baseline': getDominantBaseline(this.textBaseline)
  1059. },
  1060. true
  1061. );
  1062. if (font.family === 'topology') {
  1063. text = '--le5le--' + (+text.charCodeAt()).toString(16) + ';';
  1064. }
  1065. textElement.appendChild(this.__document.createTextNode(text));
  1066. this.__currentElement = textElement;
  1067. this.__applyStyleToCurrentElement(action);
  1068. parent.appendChild(this.__wrapTextLink(font, textElement));
  1069. };
  1070. /**
  1071. * Creates a text element
  1072. * @param text
  1073. * @param x
  1074. * @param y
  1075. */
  1076. ctx.prototype.fillText = function(text, x, y) {
  1077. this.__applyText(text, x, y, 'fill');
  1078. };
  1079. /**
  1080. * Strokes text
  1081. * @param text
  1082. * @param x
  1083. * @param y
  1084. */
  1085. ctx.prototype.strokeText = function(text, x, y) {
  1086. this.__applyText(text, x, y, 'stroke');
  1087. };
  1088. /**
  1089. * No need to implement this for svg.
  1090. * @param text
  1091. * @return {TextMetrics}
  1092. */
  1093. ctx.prototype.measureText = function(text) {
  1094. this.__ctx.font = this.font;
  1095. return this.__ctx.measureText(text);
  1096. };
  1097. /**
  1098. * Arc command!
  1099. */
  1100. ctx.prototype.arc = function(
  1101. x,
  1102. y,
  1103. radius,
  1104. startAngle,
  1105. endAngle,
  1106. counterClockwise
  1107. ) {
  1108. // in canvas no circle is drawn if no angle is provided.
  1109. if (startAngle === endAngle) {
  1110. return;
  1111. }
  1112. if (this.__currentElement.nodeName !== 'path') {
  1113. this.beginPath();
  1114. }
  1115. startAngle = startAngle % (2 * Math.PI);
  1116. endAngle = endAngle % (2 * Math.PI);
  1117. if (startAngle === endAngle) {
  1118. //circle time! subtract some of the angle so svg is happy (svg elliptical arc can't draw a full circle)
  1119. endAngle =
  1120. (endAngle + 2 * Math.PI - 0.001 * (counterClockwise ? -1 : 1)) %
  1121. (2 * Math.PI);
  1122. }
  1123. var endX = x + radius * Math.cos(endAngle),
  1124. endY = y + radius * Math.sin(endAngle),
  1125. startX = x + radius * Math.cos(startAngle),
  1126. startY = y + radius * Math.sin(startAngle),
  1127. sweepFlag = counterClockwise ? 0 : 1,
  1128. largeArcFlag = 0,
  1129. diff = endAngle - startAngle;
  1130. // https://github.com/gliffy/canvas2svg/issues/4
  1131. if (diff < 0) {
  1132. diff += 2 * Math.PI;
  1133. }
  1134. if (counterClockwise) {
  1135. largeArcFlag = diff > Math.PI ? 0 : 1;
  1136. } else {
  1137. largeArcFlag = diff > Math.PI ? 1 : 0;
  1138. }
  1139. this.lineTo(startX, startY);
  1140. this.__addPathCommand(
  1141. format(
  1142. 'A {rx} {ry} {xAxisRotation} {largeArcFlag} {sweepFlag} {endX} {endY}',
  1143. {
  1144. rx: radius,
  1145. ry: radius,
  1146. xAxisRotation: 0,
  1147. largeArcFlag: largeArcFlag,
  1148. sweepFlag: sweepFlag,
  1149. endX: endX,
  1150. endY: endY
  1151. }
  1152. )
  1153. );
  1154. this.__currentPosition = { x: endX, y: endY };
  1155. };
  1156. /**
  1157. * Generates a ClipPath from the clip command.
  1158. */
  1159. ctx.prototype.clip = function() {
  1160. var group = this.__closestGroupOrSvg(),
  1161. clipPath = this.__createElement('clipPath'),
  1162. id = randomString(this.__ids),
  1163. newGroup = this.__createElement('g');
  1164. this.__applyCurrentDefaultPath();
  1165. group.removeChild(this.__currentElement);
  1166. clipPath.setAttribute('id', id);
  1167. clipPath.appendChild(this.__currentElement);
  1168. this.__defs.appendChild(clipPath);
  1169. //set the clip path to this group
  1170. group.setAttribute('clip-path', format('url(#{id})', { id: id }));
  1171. //clip paths can be scaled and transformed, we need to add another wrapper group to avoid later transformations
  1172. // to this path
  1173. group.appendChild(newGroup);
  1174. this.__currentElement = newGroup;
  1175. };
  1176. /**
  1177. * Draws a canvas, image or mock context to this canvas.
  1178. * Note that all svg dom manipulation uses node.childNodes rather than node.children for IE support.
  1179. * http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-drawimage
  1180. */
  1181. ctx.prototype.drawImage = function() {
  1182. //convert arguments to a real array
  1183. var args = Array.prototype.slice.call(arguments),
  1184. image = args[0],
  1185. dx,
  1186. dy,
  1187. dw,
  1188. dh,
  1189. sx = 0,
  1190. sy = 0,
  1191. sw,
  1192. sh,
  1193. parent,
  1194. svg,
  1195. defs,
  1196. group,
  1197. currentElement,
  1198. svgImage,
  1199. canvas,
  1200. context,
  1201. id;
  1202. if (args.length === 3) {
  1203. dx = args[1];
  1204. dy = args[2];
  1205. sw = image.width;
  1206. sh = image.height;
  1207. dw = sw;
  1208. dh = sh;
  1209. } else if (args.length === 5) {
  1210. dx = args[1];
  1211. dy = args[2];
  1212. dw = args[3];
  1213. dh = args[4];
  1214. sw = image.width;
  1215. sh = image.height;
  1216. } else if (args.length === 9) {
  1217. sx = args[1];
  1218. sy = args[2];
  1219. sw = args[3];
  1220. sh = args[4];
  1221. dx = args[5];
  1222. dy = args[6];
  1223. dw = args[7];
  1224. dh = args[8];
  1225. } else {
  1226. throw new Error(
  1227. 'Invalid number of arguments passed to drawImage: ' + arguments.length
  1228. );
  1229. }
  1230. parent = this.__closestGroupOrSvg();
  1231. currentElement = this.__currentElement;
  1232. var translateDirective = 'translate(' + dx + ', ' + dy + ')';
  1233. if (image instanceof ctx) {
  1234. //canvas2svg mock canvas context. In the future we may want to clone nodes instead.
  1235. //also I'm currently ignoring dw, dh, sw, sh, sx, sy for a mock context.
  1236. svg = image.getSvg().cloneNode(true);
  1237. if (svg.childNodes && svg.childNodes.length > 1) {
  1238. defs = svg.childNodes[0];
  1239. while (defs.childNodes.length) {
  1240. id = defs.childNodes[0].getAttribute('id');
  1241. this.__ids[id] = id;
  1242. this.__defs.appendChild(defs.childNodes[0]);
  1243. }
  1244. group = svg.childNodes[1];
  1245. if (group) {
  1246. //save original transform
  1247. var originTransform = group.getAttribute('transform');
  1248. var transformDirective;
  1249. if (originTransform) {
  1250. transformDirective = originTransform + ' ' + translateDirective;
  1251. } else {
  1252. transformDirective = translateDirective;
  1253. }
  1254. group.setAttribute('transform', transformDirective);
  1255. parent.appendChild(group);
  1256. }
  1257. }
  1258. } else if (image.nodeName === 'CANVAS' || image.nodeName === 'IMG') {
  1259. //canvas or image
  1260. svgImage = this.__createElement('image');
  1261. svgImage.setAttribute('width', dw);
  1262. svgImage.setAttribute('height', dh);
  1263. svgImage.setAttribute('preserveAspectRatio', 'none');
  1264. if (sx || sy || sw !== image.width || sh !== image.height) {
  1265. //crop the image using a temporary canvas
  1266. canvas = this.__document.createElement('canvas');
  1267. canvas.width = dw;
  1268. canvas.height = dh;
  1269. context = canvas.getContext('2d');
  1270. context.drawImage(image, sx, sy, sw, sh, 0, 0, dw, dh);
  1271. image = canvas;
  1272. }
  1273. svgImage.setAttribute('transform', translateDirective);
  1274. var imgSrc = image.getAttribute('src');
  1275. if (imgSrc[0] === '/') {
  1276. imgSrc = location.protocol + '//' + location.host + imgSrc;
  1277. }
  1278. svgImage.setAttributeNS(
  1279. 'http://www.w3.org/1999/xlink',
  1280. 'xlink:href',
  1281. image.nodeName === 'CANVAS' ? image.toDataURL() : imgSrc
  1282. );
  1283. parent.appendChild(svgImage);
  1284. }
  1285. };
  1286. /**
  1287. * Generates a pattern tag
  1288. */
  1289. ctx.prototype.createPattern = function(image, repetition) {
  1290. var pattern = this.__document.createElementNS(
  1291. 'http://www.w3.org/2000/svg',
  1292. 'pattern'
  1293. ),
  1294. id = randomString(this.__ids),
  1295. img;
  1296. pattern.setAttribute('id', id);
  1297. pattern.setAttribute('width', image.width);
  1298. pattern.setAttribute('height', image.height);
  1299. if (image.nodeName === 'CANVAS' || image.nodeName === 'IMG') {
  1300. img = this.__document.createElementNS(
  1301. 'http://www.w3.org/2000/svg',
  1302. 'image'
  1303. );
  1304. img.setAttribute('width', image.width);
  1305. img.setAttribute('height', image.height);
  1306. img.setAttributeNS(
  1307. 'http://www.w3.org/1999/xlink',
  1308. 'xlink:href',
  1309. image.nodeName === 'CANVAS'
  1310. ? image.toDataURL()
  1311. : image.getAttribute('src')
  1312. );
  1313. pattern.appendChild(img);
  1314. this.__defs.appendChild(pattern);
  1315. } else if (image instanceof ctx) {
  1316. pattern.appendChild(image.__root.childNodes[1]);
  1317. this.__defs.appendChild(pattern);
  1318. }
  1319. return new CanvasPattern(pattern, this);
  1320. };
  1321. ctx.prototype.setLineDash = function(dashArray) {
  1322. if (dashArray && dashArray.length > 0) {
  1323. this.lineDash = dashArray.join(',');
  1324. } else {
  1325. this.lineDash = null;
  1326. }
  1327. };
  1328. /*
  1329. * Ellipse command
  1330. * @param x
  1331. * @param y
  1332. * @param radiusX
  1333. * @param radiusY
  1334. * @param startAngle
  1335. * @param endAngle
  1336. * @counterClockwise
  1337. */
  1338. ctx.prototype.ellipse = function(
  1339. x,
  1340. y,
  1341. radiusX,
  1342. radiusY,
  1343. rotation,
  1344. startAngle,
  1345. endAngle,
  1346. counterClockwise
  1347. ) {
  1348. //ellipse is the same svg command as arc, but with a radiusX and radiusY instead of just radius
  1349. if (startAngle === endAngle) {
  1350. return;
  1351. }
  1352. if (this.__currentElement.nodeName !== 'path') {
  1353. this.beginPath();
  1354. }
  1355. startAngle = startAngle % (2 * Math.PI);
  1356. endAngle = endAngle % (2 * Math.PI);
  1357. if (startAngle === endAngle) {
  1358. endAngle =
  1359. (endAngle + 2 * Math.PI - 0.001 * (counterClockwise ? -1 : 1)) %
  1360. (2 * Math.PI);
  1361. }
  1362. var endX =
  1363. x +
  1364. Math.cos(-rotation) * radiusX * Math.cos(endAngle) +
  1365. Math.sin(-rotation) * radiusY * Math.sin(endAngle),
  1366. endY =
  1367. y -
  1368. Math.sin(-rotation) * radiusX * Math.cos(endAngle) +
  1369. Math.cos(-rotation) * radiusY * Math.sin(endAngle),
  1370. startX =
  1371. x +
  1372. Math.cos(-rotation) * radiusX * Math.cos(startAngle) +
  1373. Math.sin(-rotation) * radiusY * Math.sin(startAngle),
  1374. startY =
  1375. y -
  1376. Math.sin(-rotation) * radiusX * Math.cos(startAngle) +
  1377. Math.cos(-rotation) * radiusY * Math.sin(startAngle),
  1378. sweepFlag = counterClockwise ? 0 : 1,
  1379. largeArcFlag = 0,
  1380. diff = endAngle - startAngle;
  1381. if (diff < 0) {
  1382. diff += 2 * Math.PI;
  1383. }
  1384. if (counterClockwise) {
  1385. largeArcFlag = diff > Math.PI ? 0 : 1;
  1386. } else {
  1387. largeArcFlag = diff > Math.PI ? 1 : 0;
  1388. }
  1389. this.lineTo(startX, startY);
  1390. this.__addPathCommand(
  1391. format(
  1392. 'A {rx} {ry} {xAxisRotation} {largeArcFlag} {sweepFlag} {endX} {endY}',
  1393. {
  1394. rx: radiusX,
  1395. ry: radiusY,
  1396. xAxisRotation: rotation * (180 / Math.PI),
  1397. largeArcFlag: largeArcFlag,
  1398. sweepFlag: sweepFlag,
  1399. endX: endX,
  1400. endY: endY
  1401. }
  1402. )
  1403. );
  1404. this.__currentPosition = { x: endX, y: endY };
  1405. };
  1406. /**
  1407. * Not yet implemented
  1408. */
  1409. ctx.prototype.drawFocusRing = function() {};
  1410. ctx.prototype.createImageData = function() {};
  1411. ctx.prototype.getImageData = function() {};
  1412. ctx.prototype.putImageData = function() {};
  1413. ctx.prototype.globalCompositeOperation = function() {};
  1414. ctx.prototype.setTransform = function() {};
  1415. //add options for alternative namespace
  1416. if (typeof window === 'object') {
  1417. window.C2S = ctx;
  1418. }
  1419. // CommonJS/Browserify
  1420. if (typeof module === 'object' && typeof module.exports === 'object') {
  1421. module.exports = ctx;
  1422. }
  1423. })();