Last active
August 29, 2015 14:18
-
-
Save kjlape/562b226f29607028bcd6 to your computer and use it in GitHub Desktop.
My animated Isomer playground. (http://jdan.github.io/isomer/playground)
This file contains hidden or 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
| var Point = Isomer.Point | |
| var Path = Isomer.Path | |
| var Shape = Isomer.Shape | |
| var Vector = Isomer.Vector | |
| var Color = Isomer.Color | |
| var rotationDuration = 2 * 1000 | |
| var canvas = document.getElementById("canvas") | |
| Math.TwoPI = Math.PI * 2 | |
| function updown(factor) { | |
| return 1 - Math.abs(factor - .5) | |
| } | |
| function rotate(factor) { | |
| return Math.TwoPI * factor | |
| } | |
| var paint = function(canvas, factor) { | |
| var iso = new Isomer(canvas) | |
| var red = new Color(160, 60, 50) | |
| var blue = new Color(50, 60, 160) | |
| iso.add(Shape.Prism(Point.ORIGIN, 3, 3, 1)) | |
| iso.add(Shape.Prism(Point(2, 0, 2 * factor + 1)), blue) | |
| iso.add(Shape.Prism(Point(0, 2, updown(factor) + 1)).rotateZ(Point(0.5, 2.5, 0), rotate(factor)), red) | |
| } | |
| var clear = function(canvas) { | |
| canvas.getContext('2d') | |
| .clearRect(0, 0, canvas.width, canvas.height) | |
| } | |
| var animate = function(canvas, period) { | |
| var pauseOffset = 0 | |
| var startTime = performance.now() | |
| var paused = false | |
| var lastFrame = null | |
| var tick = function(timestamp) { | |
| if (paused) { | |
| //cancelAnimationFrame(lastFrame) | |
| } else { | |
| lastFrame = requestAnimationFrame(tick) | |
| } | |
| clear(canvas) | |
| paint(canvas, ((timestamp - startTime) % period + pauseOffset) / period) | |
| } | |
| lastFrame = requestAnimationFrame(tick) | |
| return { | |
| context: this, | |
| pause: function () { | |
| if (!paused) { | |
| paused = true | |
| var now = performance.now() | |
| pauseOffset = (now - startTime + pauseOffset) % period | |
| startTime = now | |
| } | |
| }, | |
| resume: function () { | |
| if (paused) { | |
| paused = false | |
| startTime = performance.now() | |
| lastFrame = requestAnimationFrame(tick) | |
| } | |
| }, | |
| isPaused: function () { return paused }, | |
| getLastFrame: function () { return lastFrame }, | |
| startTime: startTime | |
| } | |
| } | |
| window._kaleb = window._kaleb || { animations: [], paused: [] } | |
| window._kaleb.animations.forEach(function (animation) { animation.pause && animation.pause() || window._kaleb.paused.push(animation) }) | |
| window._kaleb.animations = [] | |
| window._kaleb.animations.push(animate(canvas, rotationDuration)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment