You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

helpers.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { getWindow } from '../src/utils/window.js'
  2. import { svg } from '../src/modules/core/namespaces.js'
  3. function tag(name, attrs, children) {
  4. const doc = getWindow().document
  5. const el = doc.createElementNS(svg, name)
  6. let i
  7. for (i in attrs) {
  8. el.setAttribute(i, attrs[i])
  9. }
  10. for (i in children) {
  11. if (typeof children[i] === 'string') {
  12. children[i] = doc.createTextNode(children[i])
  13. }
  14. el.appendChild(children[i])
  15. }
  16. return el
  17. }
  18. export function fixtures() {
  19. return tag(
  20. 'svg',
  21. {
  22. height: 0,
  23. width: 0,
  24. id: 'inlineSVG'
  25. },
  26. [
  27. tag('defs', {}, [
  28. tag('linearGradient', {}, [
  29. tag('stop', { offset: '5%', 'stop-color': 'green' }),
  30. tag('stop', { offset: '95%', 'stop-color': 'gold' })
  31. ]),
  32. tag('radialGradient', {}, [
  33. tag('stop', { offset: '5%', 'stop-color': 'green' }),
  34. tag('stop', { offset: '95%', 'stop-color': 'gold' })
  35. ])
  36. ]),
  37. tag('desc', {}, ['Some description']),
  38. tag('path', {
  39. id: 'lineAB',
  40. d: 'M 100 350 l 150 -300',
  41. stroke: 'red',
  42. 'stroke-width': '3',
  43. fill: 'none'
  44. }),
  45. tag('path', {
  46. id: 'lineBC',
  47. d: 'M 250 50 l 150 300',
  48. stroke: 'red',
  49. 'stroke-width': '3',
  50. fill: 'none'
  51. }),
  52. tag('path', {
  53. d: 'M 175 200 l 150 0',
  54. stroke: 'green',
  55. 'stroke-width': '3',
  56. fill: 'none'
  57. }),
  58. tag('path', {
  59. d: 'M 100 350 q 150 -300 300 0',
  60. stroke: 'blue',
  61. 'stroke-width': '5',
  62. fill: 'none'
  63. }),
  64. tag(
  65. 'g',
  66. {
  67. stroke: 'black',
  68. 'stroke-width': '2',
  69. fill: 'black',
  70. id: 'pointGroup'
  71. },
  72. [
  73. tag('circle', {
  74. id: 'pointA',
  75. cx: '100',
  76. cy: '350',
  77. r: '3'
  78. }),
  79. tag('circle', {
  80. id: 'pointB',
  81. cx: '250',
  82. cy: '50',
  83. r: '50'
  84. }),
  85. tag('circle', {
  86. id: 'pointC',
  87. cx: '400',
  88. cy: '350',
  89. r: '50'
  90. })
  91. ]
  92. ),
  93. tag(
  94. 'g',
  95. {
  96. 'font-size': '30',
  97. font: 'sans-serif',
  98. fill: 'black',
  99. stroke: 'none',
  100. 'text-anchor': 'middle',
  101. id: 'labelGroup'
  102. },
  103. [
  104. tag(
  105. 'text',
  106. {
  107. x: '100',
  108. y: '350',
  109. dy: '-30'
  110. },
  111. ['A']
  112. ),
  113. tag(
  114. 'text',
  115. {
  116. x: '250',
  117. y: '50',
  118. dy: '-10'
  119. },
  120. ['B']
  121. ),
  122. tag(
  123. 'text',
  124. {
  125. x: '400',
  126. y: '350',
  127. dx: '30'
  128. },
  129. ['C']
  130. )
  131. ]
  132. ),
  133. tag('polygon', { points: '200,10 250,190 160,210' }),
  134. tag('polyline', { points: '20,20 40,25 60,40 80,120 120,140 200,180' })
  135. ]
  136. )
  137. }
  138. export function buildFixtures() {
  139. const doc = getWindow().document
  140. const body = doc.body || doc.documentElement
  141. const div = doc.createElement('div')
  142. div.id = 'fixtures'
  143. try {
  144. // FIXME: doesn't work in svgdom
  145. div.style.position = 'absolute'
  146. div.style.top = 0
  147. div.style.left = 0
  148. } catch (e) {
  149. //
  150. }
  151. div.appendChild(fixtures())
  152. body.appendChild(div)
  153. }
  154. export function buildCanvas() {
  155. const doc = getWindow().document
  156. const body = doc.body || doc.documentElement
  157. const div = doc.createElement('div')
  158. div.id = 'canvas'
  159. try {
  160. // FIXME: doesn't work in svgdom
  161. div.style.position = 'absolute'
  162. div.style.top = 0
  163. div.style.left = 0
  164. } catch (e) {
  165. //
  166. }
  167. body.appendChild(div)
  168. }
  169. export function clear() {
  170. const doc = getWindow().document
  171. const canvas = doc.getElementById('canvas')
  172. const fixtures = doc.getElementById('fixtures')
  173. // remove if present
  174. fixtures && fixtures.parentNode.removeChild(fixtures)
  175. canvas.parentNode.removeChild(canvas)
  176. ;[...doc.querySelectorAll('svg')].forEach((el) =>
  177. el.parentNode.removeChild(el)
  178. )
  179. }