| 1 |
# |
| 2 |
# canvas.transform library |
| 3 |
# created 12/2018 by jsb |
| 4 |
# based on plot2D.nas from the oscilloscope add-on by R. Leibner |
| 5 |
# |
| 6 |
# Contains functions to transform existing canvas elements. |
| 7 |
|
| 8 |
var transform = { |
| 9 |
_xy: func(elem, uv){ |
| 10 |
# returns [x, y]: intrinsic coords of the absolute(u, v) |
| 11 |
var (tx, ty) = elem.getTranslation(); |
| 12 |
var (sx, sy) = elem.getScale(); |
| 13 |
return [(uv[0] - tx)/sx, (uv[1] - ty)/sy]; |
| 14 |
}, |
| 15 |
|
| 16 |
move: func(elem, dx, dy){ |
| 17 |
# moves the element <dx, dy> pixels position. |
| 18 |
var (tx, ty) = elem.getTranslation(); |
| 19 |
elem.setTranslation(tx + dx, ty + dy); |
| 20 |
}, |
| 21 |
|
| 22 |
rotate: func(elem, deg, center){ |
| 23 |
# rotates the element <deg> degrees around <center>. |
| 24 |
var c = me._xy(elem, center); |
| 25 |
elem.setCenter(c).setRotation(-deg * D2R); |
| 26 |
}, |
| 27 |
|
| 28 |
flipX: func(elem, xaxis = 0) { |
| 29 |
elem.updateCenter(); |
| 30 |
var (sx, sy) = elem.getScale(); |
| 31 |
var (tx, ty) = elem.getTranslation(); |
| 32 |
var (xmin, ymin, xmax, ymax) = elem.getTightBoundingBox(); |
| 33 |
if (xaxis == 0) { |
| 34 |
xaxis = tx + sx*(xmax + xmin)/2; |
| 35 |
} |
| 36 |
elem.setScale(-sx, sy); |
| 37 |
elem.setTranslation(2*xaxis - tx, ty); |
| 38 |
return elem; |
| 39 |
}, |
| 40 |
|
| 41 |
flipY: func(elem, yaxis = 0) { |
| 42 |
elem.updateCenter(); |
| 43 |
var (sx, sy) = elem.getScale(); |
| 44 |
var (tx, ty) = elem.getTranslation(); |
| 45 |
var (xmin, ymin, xmax, ymax) = elem.getTightBoundingBox(); |
| 46 |
if (yaxis == 0) { |
| 47 |
yaxis = ty + sy*(ymax + ymin)/2; |
| 48 |
} |
| 49 |
elem.setScale(sx, -sy); |
| 50 |
elem.setTranslation(tx, 2*yaxis - ty); |
| 51 |
return elem; |
| 52 |
}, |
| 53 |
|
| 54 |
# Aligns the element, moving it horizontaly to ref. |
| 55 |
# params: |
| 56 |
# elem element to be moved. |
| 57 |
# ref reference may be an integer or another element. |
| 58 |
# alignment as string: may be 'left-left', 'left-center', 'left-right', |
| 59 |
# 'center-left', 'center-center', 'center-right', |
| 60 |
# 'right-left', 'right-center', 'right-right'. |
| 61 |
# If ref is a single number, the 2nd word is ignored. |
| 62 |
alignX: func(elem, ref, alignment) { |
| 63 |
elem.updateCenter(); |
| 64 |
var (sx, sy) = elem.getScale(); |
| 65 |
var (tx, ty) = elem.getTranslation(); |
| 66 |
var (xmin, ymin, xmax, ymax) = elem.getTightBoundingBox(); |
| 67 |
var a = split('-', alignment)[0]; |
| 68 |
var x = a == 'left' ? xmin : a == 'right' ? Xmax : (xmin + xmax)/2; |
| 69 |
if(typeof(ref) == 'scalar') var uRef = ref; |
| 70 |
else { |
| 71 |
ref.updateCenter(); |
| 72 |
var (sRx, sRy) = ref.getScale(); |
| 73 |
var (tRx, tRy) = ref.getTranslation(); |
| 74 |
var (xmin, ymin, xmax, ymax) = ref.getTightBoundingBox(); |
| 75 |
var aR = split('-', alignment)[1]; |
| 76 |
var uRef = aR =='left' ? tRx+sRx*xmin : aR =='right' ? tRx+sRx*xmax : tRx+sRx*(xmin+xmax)/2; |
| 77 |
} |
| 78 |
elem.setTranslation(uRef-x*sx, ty); |
| 79 |
return elem; |
| 80 |
}, |
| 81 |
|
| 82 |
# Aligns the element, moving it vertically to ref. |
| 83 |
# params: |
| 84 |
# elem element to be moved. |
| 85 |
# ref reference may be an integer or another element. |
| 86 |
# alignment as string: may be 'top-top', 'top-center', 'top-bottom', |
| 87 |
# 'center-top', 'center-center', 'center-bottom', |
| 88 |
# 'bottom-top', 'bottom-center', 'bottom-bottom'. |
| 89 |
# text elements also accept 'baseline' as reference. |
| 90 |
# If ref is a single number, the 2nd word is ignored. |
| 91 |
alignY: func(elem, ref, alignment) { |
| 92 |
elem.updateCenter(); |
| 93 |
var (sx, sy) = elem.getScale(); |
| 94 |
var (tx, ty) = elem.getTranslation(); |
| 95 |
var (Xmin, Ymin, Xmax, Ymax) = elem.getTightBoundingBox(); |
| 96 |
var a = split('-', alignment)[0]; |
| 97 |
var y = a == 'top' ? Ymin : a == 'bottom' ? Ymax : (Ymin+Ymax)/2; |
| 98 |
if(typeof(ref) =='scalar') var vRef = ref; |
| 99 |
else { |
| 100 |
ref.updateCenter(); |
| 101 |
var (sRx, sRy) = ref.getScale(); |
| 102 |
var (tRx, tRy) = ref.getTranslation(); |
| 103 |
var (Xmin, Ymin, Xmax, Ymax) = ref.getTightBoundingBox(); |
| 104 |
var aR = split('-', alignment)[1]; |
| 105 |
var vRef = aR =='top' ? tRy+sRy*Ymin : aR =='bottom' ? tRy+sRy*Ymax : tRy+sRy*(Ymin+Ymax)/2; |
| 106 |
} |
| 107 |
elem.setTranslation(tx, vRef-y*sy); |
| 108 |
return elem; |
| 109 |
}, |
| 110 |
|
| 111 |
# center as [x,y] in pixels, otherwise in place |
| 112 |
rotate180: func(elem, center = nil) { |
| 113 |
if(center == nil){ |
| 114 |
me.flipX(elem); |
| 115 |
me.flipY(elem); |
| 116 |
} |
| 117 |
else { |
| 118 |
me.flipX(elem, center[0]); |
| 119 |
me.flipY(elem, center[1]); |
| 120 |
} |
| 121 |
return elem; |
| 122 |
}, |
| 123 |
|
| 124 |
# Stretch element horizontally |
| 125 |
# params: |
| 126 |
# elem element to be stretched. |
| 127 |
# factor the <new-width>/<old-width> ratio. |
| 128 |
# ref the relative point to keep inplace. May be 'left', 'center' or 'right'. |
| 129 |
scaleX: func(elem, factor, ref = 'left') { |
| 130 |
elem.updateCenter(); |
| 131 |
var (sx, sy) = elem.getScale(); |
| 132 |
var (tx, ty) = elem.getTranslation(); |
| 133 |
var (xmin, ymin, xmax, ymax) = elem.getTightBoundingBox(); |
| 134 |
var x = (ref == 'left') ? xmin : (ref == 'right') ? xmax : (xmin + xmax)/2; |
| 135 |
var u = tx + x*sx; |
| 136 |
print("scaleX: "~factor~"; sx="~sx~" sy="~sy~" tx="~tx~" ty="~ty, |
| 137 |
sprintf(" BB %1.3e, %1.3e, %1.3e, %1.3e, ", xmin, ymin, xmax ,ymax), |
| 138 |
" u="~u); |
| 139 |
elem.setScale(sx*factor, sy); |
| 140 |
elem.setTranslation(u-x*sx*factor, ty); |
| 141 |
return elem; |
| 142 |
}, |
| 143 |
|
| 144 |
# strech element vertically |
| 145 |
# params: |
| 146 |
# elem element to be stretched. |
| 147 |
# factor the <new-height>/<old-height> ratio. |
| 148 |
# ref the relative point to keep inplace. May be 'top', 'center' or 'bottom'. |
| 149 |
scaleY: func(elem, factor, ref = 'top') { |
| 150 |
elem.updateCenter(); |
| 151 |
var (sx, sy) = elem.getScale(); |
| 152 |
var (tx, ty) = elem.getTranslation(); |
| 153 |
var (xmin, ymin, xmax, ymax) = elem.getTightBoundingBox(); |
| 154 |
var y = (ref =='top') ? ymin : (ref == 'bottom') ? ymax : (ymin + ymax)/2; |
| 155 |
var v = ty + y*sy; |
| 156 |
elem.setScale(sx, sy*factor); |
| 157 |
elem.setTranslation(tx, v-y*sy*factor); |
| 158 |
return elem; |
| 159 |
}, |
| 160 |
|
| 161 |
# factors as [Xfactor, Yfactor] . |
| 162 |
# ref the relative point to keep inplace: |
| 163 |
# may be 'left-top', 'left-center', 'left-bottom', |
| 164 |
# 'center-top', 'center-center', 'center-bottom', |
| 165 |
# 'right-top', 'right-center', 'right-bottom'. |
| 166 |
resize: func(elem, factors, ref = 'left-top') { |
| 167 |
me.scaleX(elem, factors[0], split('-', ref)[0]); |
| 168 |
me.scaleY(elem, factors[1], split('-', ref)[1]); |
| 169 |
return elem; |
| 170 |
}, |
| 171 |
}; |