Skip to content

Instantly share code, notes, and snippets.

@aumouvantsillage
Created November 17, 2010 12:27
Show Gist options
  • Save aumouvantsillage/703334 to your computer and use it in GitHub Desktop.
Save aumouvantsillage/703334 to your computer and use it in GitHub Desktop.
How Sozi 10.09 handles transitions from one frame to the other
Display the source blob
Display the rendered blob
Raw
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg">
<g id="container">
<rect id="source-frame" x="10" y="10" width="50" height="30"
stroke="green" fill="#ada" fill-opacity="0.5" transform="translate(70, 100) scale(4) rotate(30)" />
<rect id="target-frame" x="10" y="10" width="50" height="30"
stroke="red" fill="#daa" fill-opacity="0.5" transform="translate(400, 500) scale(6) rotate(-30)" />
</g>
<script type="application/javascript"><![CDATA[
var svgns = "http://www.w3.org/2000/svg";
var container = document.getElementById("container");
function getRectangleGeometry (id) {
// Get the coordinates and dimensions of the rectangle
var rect = document.getElementById(id);
var x = parseFloat(rect.getAttribute("x"));
var y = parseFloat(rect.getAttribute("y"));
var width = parseFloat(rect.getAttribute("width"));
var height = parseFloat(rect.getAttribute("height"));
// Get the inverse of the transformation matrix applied to the rectangle
// and compute the scale factor
var matrix = rect.getCTM().inverse();
var scale = Math.sqrt(matrix.a * matrix.a + matrix.b * matrix.b);
return {
translateX: (matrix.e - x) / scale,
translateY: (matrix.f - y) / scale,
width: width / scale,
height: height / scale,
rotate: Math.atan2(matrix.b, matrix.a) * 180 / Math.PI
};
}
var sourceFrame = getRectangleGeometry("source-frame");
var targetFrame = getRectangleGeometry("target-frame");
var startDateMs;
var endDateMs;
var horloge;
function start(dureeMs, intervalleMs) {
startDateMs = Date.now();
endDateMs = startDateMs + dureeMs;
horloge = window.setInterval(step, intervalleMs);
}
function step () {
var dateMs = Date.now();
if(dateMs >= endDateMs) {
window.clearInterval(horloge);
dateMs = endDateMs;
}
var progress = (dateMs - startDateMs) / (endDateMs - startDateMs);
var remaining = 1 - progress;
// Interpolate source and target rectangles
var inter = {};
for (var attr in sourceFrame) {
inter[attr] = sourceFrame[attr] * remaining + targetFrame[attr] * progress;
}
// Compute the coordinates and dimensions of the display area
var scale = Math.min(window.innerWidth / inter.width, window.innerHeight / inter.height);
var displayWidth = inter.width * scale;
var displayHeight = inter.height * scale;
var displayX = (window.innerWidth - displayWidth) / 2;
var displayY = (window.innerHeight - displayHeight) / 2;
// Compute the translation to perform on the document
var translateX = inter.translateX * scale + displayX;
var translateY = inter.translateY * scale + displayY;
// Perform the transformation
container.setAttribute("transform",
"translate(" + translateX + "," + translateY + ") " +
"scale(" + scale + ") " +
"rotate(" + inter.rotate + ")"
);
// Add traces of the border and the center of the display area
var m = container.getCTM().inverse();
var transform = "matrix(" + m.a + "," + m.b + "," + m.c + "," + m.d + "," + m.e + "," + m.f + ")";
var rect = document.createElementNS(svgns, "rect");
rect.setAttribute("x", displayX);
rect.setAttribute("y", displayY);
rect.setAttribute("width", displayWidth);
rect.setAttribute("height", displayHeight);
rect.setAttribute("stroke", "blue");
rect.setAttribute("fill", "none");
rect.setAttribute("transform", transform);
container.appendChild(rect);
var circle = document.createElementNS(svgns, "circle");
circle.setAttribute("cx", displayX + displayWidth / 2);
circle.setAttribute("cy", displayY + displayHeight / 2);
circle.setAttribute("r", 10);
circle.setAttribute("stroke", "blue");
circle.setAttribute("fill", "blue");
circle.setAttribute("transform", transform);
container.appendChild(circle);
if(dateMs === endDateMs) {
stop();
}
}
function stop () {
window.setTimeout(function () {
container.removeAttribute("transform");
}, 1000);
}
document.documentElement.onclick = function() {
start(3000, 40);
};
]]></script>
</svg>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment