| 1 |
#------------------------------------------------------------------------------- |
| 2 |
# Transform |
| 3 |
#------------------------------------------------------------------------------- |
| 4 |
# A transformation matrix which is used to transform an #Element on the canvas. |
| 5 |
# The dimensions of the matrix are 3x3 where the last row is always 0 0 1: |
| 6 |
# |
| 7 |
# a c e |
| 8 |
# b d f |
| 9 |
# 0 0 1 |
| 10 |
# |
| 11 |
# See http://www.w3.org/TR/SVG/coords.html#TransformMatrixDefined for details. |
| 12 |
# https://www.w3.org/TR/css-transforms-1/#mathematical-description |
| 13 |
# |
| 14 |
var Transform = { |
| 15 |
new: func(node, vals = nil) { |
| 16 |
var m = { |
| 17 |
parents: [Transform], |
| 18 |
_node: node, |
| 19 |
a: node.getNode("m[0]", 1), |
| 20 |
b: node.getNode("m[1]", 1), |
| 21 |
c: node.getNode("m[2]", 1), |
| 22 |
d: node.getNode("m[3]", 1), |
| 23 |
e: node.getNode("m[4]", 1), #tx |
| 24 |
f: node.getNode("m[5]", 1), #ty |
| 25 |
}; |
| 26 |
|
| 27 |
var use_vals = isvec(vals) and size(vals) == 6; |
| 28 |
|
| 29 |
# initialize to identity matrix |
| 30 |
m.a.setDoubleValue(use_vals ? vals[0] : 1); |
| 31 |
m.b.setDoubleValue(use_vals ? vals[1] : 0); |
| 32 |
m.c.setDoubleValue(use_vals ? vals[2] : 0); |
| 33 |
m.d.setDoubleValue(use_vals ? vals[3] : 1); |
| 34 |
m.e.setDoubleValue(use_vals ? vals[4] : 0); |
| 35 |
m.f.setDoubleValue(use_vals ? vals[5] : 0); |
| 36 |
|
| 37 |
return m; |
| 38 |
}, |
| 39 |
|
| 40 |
setTranslation: func() { |
| 41 |
var trans = _arg2valarray(arg); |
| 42 |
|
| 43 |
me.e.setDoubleValue(trans[0]); |
| 44 |
me.f.setDoubleValue(trans[1]); |
| 45 |
|
| 46 |
return me; |
| 47 |
}, |
| 48 |
|
| 49 |
getTranslation: func() { |
| 50 |
return [me.e.getValue(), me.f.getValue()]; |
| 51 |
}, |
| 52 |
|
| 53 |
# Set rotation (Optionally around a specified point instead of (0,0)) |
| 54 |
# |
| 55 |
# setRotation(rot) |
| 56 |
# setRotation(rot, cx, cy) |
| 57 |
# |
| 58 |
# @note If using with rotation center different to (0,0) do not use |
| 59 |
# #setTranslation as it would interfere with the rotation. |
| 60 |
setRotation: func(angle) { |
| 61 |
var center = _arg2valarray(arg); |
| 62 |
|
| 63 |
var s = math.sin(angle); |
| 64 |
var c = math.cos(angle); |
| 65 |
|
| 66 |
# rotation goes to the top-left 2x2 part of the matrix |
| 67 |
me.a.setDoubleValue(c); me.c.setDoubleValue(-s); |
| 68 |
me.b.setDoubleValue(s); me.d.setDoubleValue(c); |
| 69 |
|
| 70 |
if (size(center) == 2) { |
| 71 |
me.e.setDoubleValue((-center[0] * c) + (center[1] * s) + center[0]); |
| 72 |
me.f.setDoubleValue((-center[0] * s) - (center[1] * c) + center[1]); |
| 73 |
} |
| 74 |
|
| 75 |
return me; |
| 76 |
}, |
| 77 |
|
| 78 |
# Set scale (either as parameters or array) |
| 79 |
# |
| 80 |
# If only one parameter is given its value is used for both x and y |
| 81 |
# setScale(x, y) |
| 82 |
# setScale([x, y]) |
| 83 |
setScale: func { |
| 84 |
var scale = _arg2valarray(arg); |
| 85 |
# the scale factors go to the diagonal elements of the matrix |
| 86 |
me.a.setDoubleValue(scale[0]); |
| 87 |
me.d.setDoubleValue(size(scale) >= 2 ? scale[1] : scale[0]); |
| 88 |
return me; |
| 89 |
}, |
| 90 |
|
| 91 |
getScale: func() { |
| 92 |
# TODO handle rotation |
| 93 |
return [me.a.getValue(), me.d.getValue()]; |
| 94 |
}, |
| 95 |
|
| 96 |
# this function is called very often, if a canvas map is active and |
| 97 |
# the aircraft symbol is shown. |
| 98 |
setGeoPosition: func(lat, lon) { |
| 99 |
if (me["_gn"] == nil) { me._gn = me._node.getNode("m-geo[4]", 1); } |
| 100 |
if (me["_ge"] == nil) { me._ge = me._node.getNode("m-geo[5]", 1); } |
| 101 |
me._gn.setValue("N" ~ lat); |
| 102 |
me._ge.setValue("E" ~ lon); |
| 103 |
return me; |
| 104 |
}, |
| 105 |
}; |