Guest User

Untitled

a guest
Feb 18th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. export default class Transform {
  2. constructor (origin = [0, 0]) {
  3. this.origin = origin
  4. this.params = {
  5. a: 1,
  6. b: 0,
  7. c: 0,
  8. d: 1,
  9. e: 0,
  10. f: 0
  11. }
  12. }
  13.  
  14. setOrigin (origin) {
  15. if (!origin) return this
  16. this.origin = origin
  17. return this
  18. }
  19.  
  20. applyOrigin (fn, x0, y0) {
  21. if (x0 == null) x0 = this.origin[0]
  22. if (y0 == null) y0 = this.origin[1]
  23. this.params.e -= x0
  24. this.params.f -= y0
  25. fn.call(this)
  26. this.params.e += x0
  27. this.params.f += y0
  28. return this
  29. }
  30.  
  31. translate (dx = 0, dy = 0) {
  32. this.params.e += dx
  33. this.params.f += dy
  34. return this
  35. }
  36.  
  37. translateX (dx = 0) {
  38. this.params.e += dx
  39. return this
  40. }
  41.  
  42. translateY (dy = 0) {
  43. this.params.f += dy
  44. return this
  45. }
  46.  
  47. scale (sx, sy, origin = []) {
  48. if (sx == null) return this
  49. if (sy == null) sy = sx
  50. else if (typeof sy !== 'number') {
  51. origin = sy
  52. sy = sx
  53. }
  54. this.scaleX(sx, origin[0])
  55. this.scaleY(sy, origin[1])
  56. return this
  57. }
  58.  
  59. scaleX (sx, x0) {
  60. if (sx == null) return this
  61. return this.applyOrigin(function () {
  62. this.params.a *= sx
  63. this.params.c *= sx
  64. this.params.e *= sx
  65. }, x0, null)
  66. }
  67.  
  68. scaleY (sy, y0) {
  69. if (sy == null) return this
  70. return this.applyOrigin(function () {
  71. this.params.b *= sy
  72. this.params.d *= sy
  73. this.params.f *= sy
  74. }, null, y0)
  75. }
  76.  
  77. flipX (x0) {
  78. return this.scaleX(-1, x0)
  79. }
  80.  
  81. flipY (y0) {
  82. return this.scaleY(-1, y0)
  83. }
  84.  
  85. rotate180 (origin = []) {
  86. this.flipX(origin[0])
  87. this.flipY(origin[1])
  88. return this
  89. }
  90.  
  91. rotateRight (origin = []) {
  92. return this.applyOrigin(function () {
  93. const {a, b, c, d, e, f} = this.params
  94. this.params.a = -b
  95. this.params.b = a
  96. this.params.c = -d
  97. this.params.d = c
  98. this.params.e = -f
  99. this.params.f = e
  100. }, origin[0], origin[1])
  101. }
  102.  
  103. rotateLeft (origin = []) {
  104. return this.applyOrigin(function () {
  105. const {a, b, c, d, e, f} = this.params
  106. this.params.a = b
  107. this.params.b = -a
  108. this.params.c = d
  109. this.params.d = -c
  110. this.params.e = f
  111. this.params.f = -e
  112. }, origin[0], origin[1])
  113. }
  114.  
  115. invert (origin = []) {
  116. return this.applyOrigin(function () {
  117. const {a, b, c, d, e, f} = this.params
  118. this.params.a = b
  119. this.params.b = a
  120. this.params.c = d
  121. this.params.d = c
  122. this.params.e = f
  123. this.params.f = e
  124. }, origin[0], origin[1])
  125. }
  126.  
  127. rotate (A, origin = []) {
  128. if (A == null) return this
  129. const rad = A * Math.PI / 180
  130. const sinA = Math.sin(rad)
  131. const cosA = Math.cos(rad)
  132. return this.applyOrigin(function () {
  133. const {a, b, c, d, e, f} = this.params
  134. this.params.a = cosA * a - sinA * b
  135. this.params.b = sinA * a + cosA * b
  136. this.params.c = cosA * c - sinA * d
  137. this.params.d = sinA * c + cosA * d
  138. this.params.e = cosA * e - sinA * f
  139. this.params.f = sinA * e + cosA * f
  140. }, origin[0], origin[1])
  141. }
  142.  
  143. skewX (A, x0) {
  144. if (A == null) return this
  145. const tanA = Math.tan(A * Math.PI / 180)
  146. return this.applyOrigin(function () {
  147. const {a, b, c, d, e, f} = this.params
  148. this.params.a = a + tanA * b
  149. this.params.c = c + tanA * d
  150. this.params.e = e + tanA * f
  151. }, x0, null)
  152. }
  153.  
  154. skewY (A, y0) {
  155. if (A == null) return this
  156. const tanA = Math.tan(A * Math.PI / 180)
  157. return this.applyOrigin(function () {
  158. const {a, b, c, d, e, f} = this.params
  159. this.params.b = tanA * a + b
  160. this.params.d = tanA * c + d
  161. this.params.f = tanA * e + f
  162. }, null, y0)
  163. }
  164.  
  165. matrix (A, B, C, D, E, F) {
  166. const {a, b, c, d, e, f} = this.params
  167. this.params.a = A * a + C * b
  168. this.params.b = B * a + D * b
  169. this.params.c = A * c + C * d
  170. this.params.d = B * c + D * d
  171. this.params.e = A * e + C * f + E
  172. this.params.f = B * e + D * f + F
  173. return this
  174. }
  175.  
  176. clone () {
  177. const t = new Transform(this.origin)
  178. Object.assign(t.params, this.params)
  179. return t
  180. }
  181.  
  182. inverse () {
  183. const t = new Transform(this.origin)
  184. t.params = this.complement
  185. return t
  186. }
  187.  
  188. chain (transform) {
  189. const t = this.clone()
  190. const {a, b, c, d, e, f} = transform.params
  191. return t.matrix(a, b, c, d, e, f)
  192. }
  193.  
  194. decompose () {
  195. const {a, b, c, d, e, f} = this.params
  196. const decomposed = [new Transform(), new Transform()]
  197. Object.assign(decomposed[0].params, {a, b, c, d})
  198. Object.assign(decomposed[1].params, {e, f})
  199. return decomposed
  200. }
  201.  
  202. textCorrection (origin = [0, 0]) {
  203. return new Transform()
  204. .translate(-origin[0], -origin[1])
  205. .chain(this.inverse().decompose()[0])
  206. .translate(origin[0], origin[1])
  207. }
  208.  
  209. get complement () {
  210. const {a, b, c, d, e, f} = this.params
  211. const disc = a * d - b * c
  212. return {
  213. a: d / disc,
  214. b: -b / disc,
  215. c: -c / disc,
  216. d: a / disc,
  217. e: (c * f - d * e) / disc,
  218. f: (b * e - a * f) / disc
  219. }
  220. }
  221.  
  222. get apply () {
  223. return ([x, y]) => {
  224. const {a, b, c, d, e, f} = this.params
  225. return [
  226. a * x + c * y + e,
  227. b * x + d * y + f
  228. ]
  229. }
  230. }
  231.  
  232. get unapply () {
  233. return ([x, y]) => {
  234. const {a, b, c, d, e, f} = this.complement
  235. return [
  236. a * x + c * y + e,
  237. b * x + d * y + f
  238. ]
  239. }
  240. }
  241.  
  242. toString (dp = 5) {
  243. const {a, b, c, d, e, f} = this.params
  244. return `matrix(${[a, b, c, d, e, f].map(v => v.toFixed(dp)).join(' ')})`
  245. }
  246. }
  247.  
  248. /*
  249. const test = new Transform()
  250. test.params = {
  251. a: 1,
  252. b: 2,
  253. c: 3,
  254. d: 4,
  255. e: 5,
  256. f: 6
  257. }
  258.  
  259. const original = [1, 1]
  260. const applied = test.apply(original)
  261. const reversed = test.unapply(applied)
  262. console.log(original, applied, reversed)
  263. */
Add Comment
Please, Sign In to add comment