16
AnimateClip
Objective
- Responder
- View
- Clip
- AnimateClip
- Clip
- View
Move the pointer of the mouse over the clip. Click in the clip or press the space bar to start playing it. Click or press again the space bar to pause it, continue to play it. Use the right and left arrows to move forward or backward by one second, press the Shift key or the Ctrl key at the same time to move forward or backward by ten seconds or a hundred milliseconds. Press the 0 key to come back to the beginning. Press the plus key to play the clip faster, the minus key to play it slower, the asterisk key to play it at the normal speed.
- function AnimateClip() {
- Clip.call(this);
- this._animlist = null;
- this._lastanim = null;
- }
- AnimateClip.prototype = Object.create(Clip.prototype);
- Object.defineProperty(AnimateClip.prototype, 'constructor', { value: AnimateClip, enumerable: false, writable: true });
- Object.defineProperty(AnimateClip.prototype, 'currentTime', {
- get: function() {
- return this._animlist ? Math.floor(this._lastanim.currentTime) : 0;
- }
- });
- Object.defineProperty(AnimateClip.prototype, 'playbackRate', {
- get: function() {
- return this._playbackRate;
- },
- set: function(r) {
- if (typeof r !== 'number')
- throw new TypeError();
- if (r < Clip.minPlaybackRate || r > Clip.maxPlaybackRate)
- throw new RangeError();
- this._playbackRate = r;
- if (this._animlist)
- for (let e of this._animlist)
- e.playbackRate = r;
- }
- });
- AnimateClip.prototype.animate = function(animations, autoplay = false) {
- if (! (Array.isArray(animations) && animations.length > 0))
- throw new TypeError();
- if (this._animlist)
- for (let e of this._animlist)
- e.cancel();
- let duration = 0, lastanim;
- const animlist = [];
- for (let e of animations) {
- if (! (Array.isArray(e) && e.length == 3))
- throw new TypeError();
- let [widget, keyframes, options] = e;
- options.playbackRate = this._playbackRate;
- let anim = widget.animate(keyframes, options);
- let ms = (options.delay || 0) + options.duration * (options.iterations || 1) + (options.endDelay || 0);
- if (ms > duration) {
- lastanim = anim;
- duration = ms;
- }
- animlist.push(anim);
- }
- lastanim.onfinish = () => {
- this._ended = this._paused = true;
- this.notify('clipEnded', this);
- };
- this._duration = duration;
- this._animlist = animlist;
- this._lastanim = lastanim;
- if (!autoplay)
- for (let e of animlist)
- e.pause();
- this._paused = !autoplay;
- this._ended = false;
- return this;
- };
- AnimateClip.prototype.seek = function(ms) {
- if (!this._animlist)
- return this;
- for (let e of this._animlist) {
- e.currentTime = ms;
- if (this._paused)
- e.pause();
- }
- this.notify('clipSeeked', this);
- return this;
- };
- AnimateClip.prototype.play = function() {
- if (!this._animlist)
- return this;
- for (let e of this._animlist)
- if (this._ended || e.playState != 'finished')
- e.play();
- this._ended = this._paused = false;
- return this;
- };
- AnimateClip.prototype.pause = function() {
- if (!this._animlist)
- return this;
- for (let e of this._animlist)
- if (e.playState != 'finished')
- e.pause();
- this._paused = true;
- return this;
- };
Test
- <?php $text='Objective.js'; ?>
- <?php $font='Slackey'; ?>
- <?php $fontSize=60; ?>
- <?php $color='#3bc'; ?>
- <?php head('font', $font); ?>
- <?php $id=uniqid('id'); ?>
- .test_display {
- display: inline-block;
- color: <?php echo $color; ?>;
- font-family: "<?php echo $font; ?>", sans-serif;
- font-size: <?php echo $fontSize; ?>px;
- user-select: none;
- }
- .test_display .letter {
- display: inline-block;
- transform-origin: 50% 100%;
- }
- <div id="<?php echo $id; ?>" class="noprint">
- <div class="test_display"><div><?php echo $text; ?></div></div>
- </div>
- <?php head('javascript', '/objectivejs/Objective.js'); ?>
- <?php head('javascript', '/objectivejs/Responder.js'); ?>
- <?php head('javascript', '/objectivejs/View.js'); ?>
- <?php head('javascript', '/objectivejs/Clip.js'); ?>
- <?php head('javascript', '/objectivejs/AnimateClip.js'); ?>
- const container = document.querySelector('#<?php echo $id; ?>');
- const display = container.querySelector('.test_display');
- const wrapper = display.querySelector('div');
- wrapper.innerHTML = wrapper.textContent.replace(/\S/g, "<span class='letter'>$&</span>");
- const clip = new AnimateClip();
- clip.setWidget(display);
- const duration = 2000, delay = 1000;
- const animations = [];
- const letters = wrapper.querySelectorAll('.letter');
- for (let i = 0; i < letters.length; i++) {
- let keyframes = [ {transform: 'scale(0)'}, {transform: 'scale(1)'} ];
- let options = {delay: delay + 50 * i, duration: duration, easing: 'ease-in-out'};
- animations.push([letters[i], keyframes, options]);
- }
- const text_keyframes = [ {opacity: 1}, {opacity: 0} ];
- const text_options = {delay: delay + duration + letters.length * 50, duration: 1000, easing: 'ease-in-out'};
- animations.push([wrapper, text_keyframes, text_options]);
- clip.animate(animations);
- clip.enablePlayer();
Comments