/
SVGBaseLayer.coffee
105 lines (91 loc) · 3.35 KB
/
SVGBaseLayer.coffee
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{Layer, layerProperty} = require "./Layer"
{Color} = require "./Color"
class exports.SVGBaseLayer extends Layer
# Overridden Layer properties
@define "parent",
enumerable: false
exportable: false
importable: false
get: ->
@_parent or null
@define "html", get: -> @_element.outerHTML or ""
@define "width", get: -> @_width
@define "height", get: -> @_height
# Disabled properties
@undefine ["label", "blending", "image"]
@undefine ["blur", "brightness", "saturate", "hueRotate", "contrast", "invert", "grayscale", "sepia"] # webkitFilter properties
@undefine ["backgroundBlur", "backgroundBrightness", "backgroundSaturate", "backgroundHueRotate", "backgroundContrast", "backgroundInvert", "backgroundGrayscale", "backgroundSepia"] # webkitBackdropFilter properties
for i in [0..8]
do (i) =>
@undefine "shadow#{i+1}"
@undefine "shadows"
@undefine ["borderRadius", "cornerRadius", "borderStyle"]
@undefine ["constraintValues", "htmlIntrinsicSize"]
# Aliassed helpers
@alias = (propertyName, proxiedName) ->
@define propertyName,
get: ->
@[proxiedName]
set: (value) ->
return if @__applyingDefaults
@[proxiedName] = value
@alias "borderColor", "stroke"
@alias "strokeColor", "stroke"
@alias "borderWidth", "strokeWidth"
@alias "backgroundColor", "fill"
# Overridden functions from Layer
_insertElement: ->
updateForSizeChange: ->
updateForDevicePixelRatioChange: =>
for cssProperty in ["width", "height", "webkitTransform"]
@_element.style[cssProperty] = LayerStyle[cssProperty](@)
copy: undefined
copySingle: undefined
addChild: undefined
removeChild: undefined
addSubLayer: undefined
removeSubLayer: undefined
bringToFront: undefined
sendToBack: undefined
placeBefore: undefined
placeBehind: undefined
@attributesFromElement: (attributes, element) ->
options = {}
for attribute in attributes
key = _.camelCase attribute
options[key] = element.getAttribute(attribute)
return options
constructor: (options) ->
element = options.element
@_element = element
@_elementBorder = element
@_elementHTML = element
@_parent = options.parent
delete options.parent
delete options.element
pathProperties = ["fill", "stroke", "stroke-width", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-dasharray", "stroke-dashoffset", "name"]
_.defaults options, @constructor.attributesFromElement(pathProperties, element)
if @_element.transform.baseVal.numberOfItems > 0
translate = @_element.transform.baseVal.getItem(0).matrix
options.x ?= translate.e
options.y ?= translate.f
super(options)
rect = @_element.getBoundingClientRect()
@_width = rect.width * @context.pixelMultiplier
@_height = rect.height * @context.pixelMultiplier
for parent in @ancestors()
if parent instanceof SVGLayer
@_svg = parent.svg
break
@resetViewbox()
for prop in ["frame", "stroke", "strokeWidth", "strokeLinecap", "strokeLinejoin", "strokeMiterlimit", "strokeDasharray", "strokeDashoffset", "rotation", "scale"]
@on "change:#{prop}", @resetViewbox
@define "gradient",
get: ->
console.warn "The gradient property is currently not supported on shapes"
return undefined
set: (value) ->
console.warn "The gradient property is currently not supported on shapes"
resetViewbox: =>
@_svg.setAttribute("viewBox", "0,0,#{@width},#{@height}")
@_svg.removeAttribute("viewBox")