|
| 1 | + |
| 2 | +<template> |
| 3 | + <div class="mojs-interactive"> |
| 4 | + <slot></slot> |
| 5 | + <div class="mojs-interactive__result" id="demo-expanding-circle" style="background: rgb(241, 226, 215); height: 600px"> |
| 6 | + <button class="button button--icon button--control" v-on:click="playPause" :aria-label="isPlaying ? 'Pause animation' : 'Play animation'">{{isPlaying ? '⇤' : '︎︎︎▶︎'}}</button> |
| 7 | + <div id="demo-expanding-circle_controller" class="mojs-interactive__controller"></div> |
| 8 | + </div> |
| 9 | + </div> |
| 10 | +</template> |
| 11 | + |
| 12 | +<script> |
| 13 | + export default { |
| 14 | + data: function () { |
| 15 | + return { |
| 16 | + isPlaying: false, |
| 17 | + prefersReducedMotion: false, |
| 18 | + } |
| 19 | + }, |
| 20 | +
|
| 21 | + methods: { |
| 22 | + playPause: function() { |
| 23 | + if (this.isPlaying) { |
| 24 | + this.timeline.replayBackward( |
| 25 | + this.prefersReducedMotion ? 1800 : 0 |
| 26 | + ); |
| 27 | + } else { |
| 28 | + this.timeline.play( |
| 29 | + this.prefersReducedMotion ? 1700 : 0 |
| 30 | + ); |
| 31 | + } |
| 32 | + this.isPlaying = !this.isPlaying; |
| 33 | + } |
| 34 | + }, |
| 35 | +
|
| 36 | + mounted: function () { |
| 37 | + var motionQuery = matchMedia('(prefers-reduced-motion)'); |
| 38 | + const handleReduceMotionChanged = () => { |
| 39 | + this.prefersReducedMotion = motionQuery.matches; |
| 40 | + } |
| 41 | + motionQuery.addListener(handleReduceMotionChanged); |
| 42 | + handleReduceMotionChanged(); // trigger once on load to check initial value |
| 43 | +
|
| 44 | + import("mo-js").then(module => { |
| 45 | + import('mojs-player').then(module => { |
| 46 | +
|
| 47 | +
|
| 48 | + const COLORS = { |
| 49 | + bg: 'rgb(241, 226, 215)', |
| 50 | + accent: '#3A0839', |
| 51 | + }; |
| 52 | +
|
| 53 | + const _getWindowSize = () => { |
| 54 | + const w = window; |
| 55 | + return Math.max( (w.innerHeight || e.clientHeight || g.clientHeight), (w.innerWidth || e.clientWidth || g.clientWidth) ) |
| 56 | + } |
| 57 | +
|
| 58 | + const _calcScale = (radius) => { |
| 59 | + return 1.4 * (_getWindowSize() / radius / 2); |
| 60 | + } |
| 61 | +
|
| 62 | + let circleSize = 500, |
| 63 | + scale = _calcScale( circleSize ), |
| 64 | + opts = { |
| 65 | + parent: '#demo-expanding-circle', |
| 66 | + radius: circleSize, |
| 67 | + fill: COLORS.accent, |
| 68 | + scale: { 0.05 : 0.2 }, |
| 69 | + duration: 465, |
| 70 | + easing: 'cubic.out', |
| 71 | + isShowEnd: false, |
| 72 | + isForce3d: true, |
| 73 | + }; |
| 74 | +
|
| 75 | + const circle1 = new mojs.Shape(opts) |
| 76 | + .then({ |
| 77 | + easing: 'cubic.inout', |
| 78 | + scale: .125, |
| 79 | + duration: 350 |
| 80 | + }).then({ |
| 81 | + easing: 'cubic.inout', |
| 82 | + scale: scale, |
| 83 | + duration: 465 |
| 84 | + }); |
| 85 | +
|
| 86 | + const circle2 = new mojs.Shape({ |
| 87 | + ...opts, |
| 88 | + fill: COLORS.bg, |
| 89 | + scale: { 0 : .1125 }, |
| 90 | + duration: 407, |
| 91 | + delay: 580 |
| 92 | + }).then({ |
| 93 | + easing: 'cubic.inout', |
| 94 | + scale: scale, |
| 95 | + duration: 350 |
| 96 | + }); |
| 97 | +
|
| 98 | + const circle3 = new mojs.Shape({ |
| 99 | + ...opts, |
| 100 | + scale: { 0: scale }, |
| 101 | + duration: 580, |
| 102 | + delay: 1160, |
| 103 | + isShowStart: false, |
| 104 | + isShowEnd: true |
| 105 | + }); |
| 106 | +
|
| 107 | + const smallCircle = new mojs.Shape({ |
| 108 | + parent: '#demo-expanding-circle', |
| 109 | + radius: { 5: 25 }, |
| 110 | + fill: 'none', |
| 111 | + stroke: COLORS.accent, |
| 112 | + strokeWidth: { 20: 0 }, |
| 113 | + isShowEnd: false, |
| 114 | + delay: 700, |
| 115 | + duration: 290, |
| 116 | + }); |
| 117 | +
|
| 118 | + const triangleOpts = { |
| 119 | + parent: '#demo-expanding-circle', |
| 120 | + shape: 'polygon', |
| 121 | + radius: 15, |
| 122 | + duration: 407, |
| 123 | + fill: COLORS.bg, |
| 124 | + y: { 80 : -15 }, |
| 125 | + scale: { 1 : 0 }, |
| 126 | + delay: 495, |
| 127 | + isForce3d: true, |
| 128 | + easing: 'cubic.out' |
| 129 | + } |
| 130 | + const triangle1 = new mojs.Shape( triangleOpts ); |
| 131 | +
|
| 132 | + const triangle2 = new mojs.Shape({ |
| 133 | + ...triangleOpts, |
| 134 | + y: { [-80] : 15 }, |
| 135 | + angle: 180 |
| 136 | + }); |
| 137 | +
|
| 138 | + const lineOpts = { |
| 139 | + parent: '#demo-expanding-circle', |
| 140 | + shape: 'line', |
| 141 | + x: -180, |
| 142 | + radius: 50, |
| 143 | + radiusY: 0, |
| 144 | + stroke: COLORS.accent, |
| 145 | + strokeWidth: {15 : 0}, |
| 146 | + duration: 580, |
| 147 | + isShowEnd: false, |
| 148 | + strokeDasharray: '100% 100%', |
| 149 | + strokeDashoffset: { '-100%': '100%' }, |
| 150 | + easing: 'cubic.out', |
| 151 | + } |
| 152 | + const line1 = new mojs.Shape(lineOpts); |
| 153 | +
|
| 154 | + const line2 = new mojs.Shape({ |
| 155 | + ...lineOpts, |
| 156 | + angle: 180, |
| 157 | + x: -lineOpts.x |
| 158 | + }); |
| 159 | +
|
| 160 | + this.timeline = new mojs.Timeline(); |
| 161 | + this.timeline |
| 162 | + .add( |
| 163 | + circle1, circle2, circle3, smallCircle, |
| 164 | + triangle1, triangle2, |
| 165 | + line1, line2 |
| 166 | + ); |
| 167 | +
|
| 168 | + const parentDOM = document.getElementById('demo-expanding-circle_controller'); |
| 169 | + new MojsPlayer({ |
| 170 | + add: this.timeline, |
| 171 | + isPlaying: false, |
| 172 | + isRepeat: false, |
| 173 | + parent: parentDOM, |
| 174 | + name: "demo-expanding-circle_controller", |
| 175 | + className: 'controller', |
| 176 | + isSaveState: false, |
| 177 | + isHidden: true, |
| 178 | + }); |
| 179 | +
|
| 180 | + }); |
| 181 | +
|
| 182 | + }); |
| 183 | +
|
| 184 | + }, |
| 185 | + } |
| 186 | +</script> |
0 commit comments