-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #6 - add interpolateTransformCss.
- Loading branch information
Showing
7 changed files
with
131 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
var rad2deg = 180 / Math.PI; | ||
|
||
export default function(matrix) { | ||
var r0 = [matrix.a, matrix.b], | ||
r1 = [matrix.c, matrix.d], | ||
kx = normalize(r0), | ||
kz = dot(r0, r1), | ||
ky = normalize(combine(r1, r0, -kz)) || 0; | ||
|
||
if (r0[0] * r1[1] < r1[0] * r0[1]) r0[0] *= -1, r0[1] *= -1, kx *= -1, kz *= -1; | ||
|
||
return { | ||
rotate: (kx ? Math.atan2(r0[1], r0[0]) : Math.atan2(-r1[0], r1[1])) * rad2deg, | ||
translate: [matrix.e, matrix.f], | ||
scale: [kx, ky], | ||
skew: ky ? Math.atan2(kz, ky) * rad2deg : 0 | ||
}; | ||
} | ||
|
||
function dot(a, b) { | ||
return a[0] * b[0] + a[1] * b[1]; | ||
} | ||
|
||
function normalize(a) { | ||
var k = Math.sqrt(dot(a, a)); | ||
if (k) a[0] /= k, a[1] /= k; | ||
return k; | ||
} | ||
|
||
function combine(a, b, k) { | ||
a[0] += k * b[0]; | ||
a[1] += k * b[1]; | ||
return a; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import number from "../number"; | ||
import {parseCss, parseSvg} from "./parse"; | ||
|
||
function interpolateTransform(parse, pxComma, pxParen, degParen) { | ||
|
||
function pop(s) { | ||
return s.length ? s.pop() + " " : ""; | ||
} | ||
|
||
function translate(ta, tb, s, q) { | ||
if (ta[0] !== tb[0] || ta[1] !== tb[1]) { | ||
var i = s.push("translate(", null, pxComma, null, pxParen); | ||
q.push({i: i - 4, x: number(ta[0], tb[0])}, {i: i - 2, x: number(ta[1], tb[1])}); | ||
} else if (tb[0] || tb[1]) { | ||
s.push("translate(" + tb[0] + pxComma + tb[1] + pxParen); | ||
} | ||
} | ||
|
||
function rotate(ra, rb, s, q) { | ||
if (ra !== rb) { | ||
if (ra - rb > 180) rb += 360; else if (rb - ra > 180) ra += 360; // shortest path | ||
q.push({i: s.push(pop(s) + "rotate(", null, degParen) - 2, x: number(ra, rb)}); | ||
} else if (rb) { | ||
s.push(pop(s) + "rotate(" + rb + degParen); | ||
} | ||
} | ||
|
||
function skew(wa, wb, s, q) { | ||
if (wa !== wb) { | ||
q.push({i: s.push(pop(s) + "skewX(", null, degParen) - 2, x: number(wa, wb)}); | ||
} else if (wb) { | ||
s.push(pop(s) + "skewX(" + wb + degParen); | ||
} | ||
} | ||
|
||
function scale(ka, kb, s, q) { | ||
if (ka[0] !== kb[0] || ka[1] !== kb[1]) { | ||
var i = s.push(pop(s) + "scale(", null, ",", null, ")"); | ||
q.push({i: i - 4, x: number(ka[0], kb[0])}, {i: i - 2, x: number(ka[1], kb[1])}); | ||
} else if (kb[0] !== 1 || kb[1] !== 1) { | ||
s.push(pop(s) + "scale(" + kb + ")"); | ||
} | ||
} | ||
|
||
return function(a, b) { | ||
var s = [], // string constants and placeholders | ||
q = []; // number interpolators | ||
a = parse(a), b = parse(b); | ||
translate(a.translate, b.translate, s, q); | ||
rotate(a.rotate, b.rotate, s, q); | ||
skew(a.skew, b.skew, s, q); | ||
scale(a.scale, b.scale, s, q); | ||
a = b = null; // gc | ||
return function(t) { | ||
var i = -1, n = q.length, o; | ||
while (++i < n) s[(o = q[i]).i] = o.x(t); | ||
return s.join(""); | ||
}; | ||
}; | ||
} | ||
|
||
export var interpolateTransformCss = interpolateTransform(parseCss, "px, ", "px)", "deg)"); | ||
export var interpolateTransformSvg = interpolateTransform(parseSvg, ", ", ")", ")"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import decompose from "./decompose"; | ||
|
||
var identity = {a: 1, b: 0, c: 0, d: 1, e: 0, f: 0}, | ||
cssNode, | ||
cssRoot, | ||
cssView, | ||
svgNode; | ||
|
||
export function parseCss(value) { | ||
if (value === "none") return decompose(identity); | ||
if (!cssNode) cssNode = document.createElement("DIV"), cssRoot = document.documentElement, cssView = document.defaultView; | ||
cssNode.style.transform = value; | ||
cssRoot.appendChild(cssNode); | ||
value = cssView.getComputedStyle(cssNode, null).getPropertyValue("transform"); | ||
cssRoot.removeChild(cssNode); | ||
var m = value.slice(7, -1).split(","); | ||
return decompose({a: +m[0], b: +m[1], c: +m[2], d: +m[3], e: +m[4], f: +m[5]}); | ||
}; | ||
|
||
export function parseSvg(value) { | ||
if (!svgNode) svgNode = document.createElementNS("http://www.w3.org/2000/svg", "g"); | ||
svgNode.setAttribute("transform", value == null ? "" : value); | ||
return decompose(svgNode.transform.baseVal.consolidate().matrix); | ||
}; |