ZZJ
2021-11-08 e80763e664be64ea36ba367c15c615df78d77aa7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
export default {
  randomId () {
    return Math.random().toString(36).substring(7)
  },
 
  // gets canvas style from css properties
  fillStyle (style, svg) {
    let pseudo = null
    let id = 'picker-' + this.randomId()
    let el = this.canvasPicker(style, id)
    // to replace 'px'
    svg.appendChild(el)
    let props = {
      fillStyle: 'fill',
      strokeStyle: 'stroke',
      lineWidth: 'stroke-width',
      fontFamily: 'font-family'
    }
    style = this.mapStyle(id, props, style, pseudo)
    svg.removeChild(el)
    return style
  },
  mapStyle (id, props, style, pseudo, numberValues) {
    let cStyle = window.getComputedStyle(document.getElementById(id), pseudo)
    numberValues = numberValues || ['lineWidth']
    for (let p in props) {
      let value = cStyle.getPropertyValue(props[p])
      if (numberValues.indexOf(p) > -1) value = parseInt(value, 10)
      if (value) {
        style[p] = value
      }
    }
    return style
  },
  // creates svg elements to pick css properties
  canvasPicker (style, id) {
    let attrs = style._svgAttrs || {}
    let element = style._svgElement || 'circle'
    if (!style._svgAttrs) {
      switch (element) {
      case 'text':
        attrs = { x: 10, y: 10, fontSize: 20 }
        break
      case 'circle':
        attrs = { cx: 10, cy: 10, r: 10 }
        break
      }
    }
    attrs.class = style._cssClass
    attrs.id = id
    return this.svgCreate(element, attrs)
  },
  compColor (color) {
    let el = document.createElement('div')
    el.style.backgroundColor = color
    document.body.appendChild(el)
    let nColor = window.getComputedStyle(el, null).getPropertyValue('background-color')
    document.body.removeChild(el)
    return nColor
  },
  // creates svg element
  svgCreate (element, attrs) {
    let el = document.createElementNS('http://www.w3.org/2000/svg', element)
    for (let a in attrs) {
      el.setAttributeNS(null, a, attrs[a])
    }
    return el
  },
  create (element, idPref, appendTo) {
    appendTo = appendTo || 'body'
    let el = document.createElement(element)
    let id = idPref || ''
    id += this.randomId()
    el.setAttribute('id', id)
    document[appendTo].appendChild(el)
    return el
  }
}