|
| 1 | + |
| 2 | + |
| 3 | +/** |
| 4 | + * Note: |
| 5 | + * |
| 6 | + * @typedef { import('../../typescript-definitions/src/main').GraphicsAPI.Graphic } Graphic |
| 7 | + */ |
| 8 | + |
| 9 | +/** @implements { Graphic } */ |
| 10 | +class MyGraphic extends HTMLElement { |
| 11 | + constructor() { |
| 12 | + super(); |
| 13 | + this.nonRealTimeState = {}; |
| 14 | + } |
| 15 | + connectedCallback() { |
| 16 | + // Called when the element is added to the DOM |
| 17 | + // Note: Don't paint any pixels at this point, wait for load() to be called |
| 18 | + } |
| 19 | + |
| 20 | + async load(loadParams) { |
| 21 | + // 1. Load resources |
| 22 | + // 2. Setup the DOM |
| 23 | + // 3. Initialize the GSAP timeline |
| 24 | + |
| 25 | + // Load the GSAP scripts --------------------------------------------------- |
| 26 | + const importsPromises = { |
| 27 | + gsap: import(import.meta.resolve("./lib/gsap-core.js")), |
| 28 | + CSSPlugin: import(import.meta.resolve("./lib/CSSPlugin.js")), |
| 29 | + TextPlugin: import(import.meta.resolve("./lib/TextPlugin.js")), |
| 30 | + }; |
| 31 | + |
| 32 | + this.g = await importsPromises.gsap; |
| 33 | + this.g.gsap.registerPlugin((await importsPromises.CSSPlugin).CSSPlugin); |
| 34 | + this.g.gsap.registerPlugin((await importsPromises.TextPlugin).TextPlugin); |
| 35 | + |
| 36 | + // Setup our DOM ----------------------------------------------------------- |
| 37 | + const container = document.createElement("div"); |
| 38 | + this.appendChild(container); |
| 39 | + |
| 40 | + container.style.position = "absolute"; |
| 41 | + container.style.left = "10%"; |
| 42 | + container.style.bottom = "10%"; |
| 43 | + container.style.padding = "10px 20px"; |
| 44 | + container.style.backgroundColor = "#f00"; |
| 45 | + container.style.color = "#fff"; |
| 46 | + container.style.fontFamily = "Roboto, sans-serif"; |
| 47 | + container.style.fontSize = "24px"; |
| 48 | + container.style.fontWeight = "bold"; |
| 49 | + |
| 50 | + const nameText = document.createElement("div"); |
| 51 | + nameText.innerText = ""; |
| 52 | + container.appendChild(nameText); |
| 53 | + |
| 54 | + this.elements = { |
| 55 | + container, |
| 56 | + nameText, |
| 57 | + }; |
| 58 | + |
| 59 | + // Initialize the GSAP timeline -------------------------------------------- |
| 60 | + this.timeline = this.g.gsap.timeline(); |
| 61 | + this._resetTimeline(); |
| 62 | + |
| 63 | + if (loadParams.renderType === "realtime") { |
| 64 | + this.timeline.play(); // not really needed, it's playing by default |
| 65 | + } else if (loadParams.renderType === "non-realtime") { |
| 66 | + this.timeline.pause(); |
| 67 | + } else throw new Error("Unsupported renderType: " + loadParams.renderType); |
| 68 | + |
| 69 | + // When everything is loaded we can return: |
| 70 | + return; |
| 71 | + } |
| 72 | + async dispose(_payload) { |
| 73 | + this.innerHTML = ""; |
| 74 | + this.g = null; |
| 75 | + } |
| 76 | + async getStatus(_payload) { |
| 77 | + return {}; |
| 78 | + } |
| 79 | + async updateAction(params) { |
| 80 | + // params.data |
| 81 | + // console.log("params", params); |
| 82 | + |
| 83 | + await this._doAction("update", params); |
| 84 | + } |
| 85 | + async playAction(params) { |
| 86 | + // params.delta |
| 87 | + // params.goto |
| 88 | + // params.skipAnimation |
| 89 | + |
| 90 | + await this._doAction("play"); |
| 91 | + } |
| 92 | + async stopAction(params) { |
| 93 | + // params.skipAnimation |
| 94 | + |
| 95 | + await this._doAction("stop"); |
| 96 | + } |
| 97 | + async customAction(params) { |
| 98 | + // params.method |
| 99 | + // params.payload |
| 100 | + |
| 101 | + await this._doAction(params.method, params.payload); |
| 102 | + } |
| 103 | + async _doAction(method, payload) { |
| 104 | + const timeline = this.g.gsap.timeline(); |
| 105 | + |
| 106 | + // Retrieve the tweens for the action: |
| 107 | + const tweens = this._getActionAnimation(method, payload); |
| 108 | + |
| 109 | + // Add the tweens to the timeline, so that they'll animate: |
| 110 | + for (const tween of tweens) { |
| 111 | + timeline.add(tween, 0); |
| 112 | + } |
| 113 | + // Wait for timeline to finish animating: |
| 114 | + await timeline.then(); |
| 115 | + } |
| 116 | + async goToTime(payload) { |
| 117 | + this.nonRealTimeState.timestamp = payload.timestamp; |
| 118 | + |
| 119 | + await this._updateState(); |
| 120 | + } |
| 121 | + async setActionsSchedule(payload) { |
| 122 | + this.nonRealTimeState.schedule = payload.schedule; |
| 123 | + |
| 124 | + await this._updateState(); |
| 125 | + } |
| 126 | + async _updateState() { |
| 127 | + this._resetTimeline(); |
| 128 | + |
| 129 | + // Initial state: |
| 130 | + |
| 131 | + for (const action of this.nonRealTimeState.schedule) { |
| 132 | + const tweens = this._getActionAnimation( |
| 133 | + action.invokeAction.method, |
| 134 | + action.invokeAction.payload |
| 135 | + ); |
| 136 | + |
| 137 | + for (const tween of tweens) { |
| 138 | + this.timeline.add(tween, action.timestamp / 1000); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + this.timeline.seek(this.nonRealTimeState.timestamp / 1000); |
| 143 | + } |
| 144 | + _getActionAnimation(method, payload) { |
| 145 | + switch (method) { |
| 146 | + case "update": |
| 147 | + return this._getUpdateAnimation(payload); |
| 148 | + case "play": |
| 149 | + return this._getPlayAnimation(payload); |
| 150 | + case "stop": |
| 151 | + return this._getStopAnimation(payload); |
| 152 | + case "highlight": |
| 153 | + return this._getHighlightAnimation(payload); |
| 154 | + default: |
| 155 | + throw new Error(`Unknown method "${method}"`); |
| 156 | + } |
| 157 | + } |
| 158 | + _resetTimeline() { |
| 159 | + const gsap = this.g.gsap; |
| 160 | + |
| 161 | + // Clear the timeline: |
| 162 | + this.timeline.clear(); |
| 163 | + |
| 164 | + // Initial animation state: |
| 165 | + const tweens = [ |
| 166 | + gsap.set(this.elements.container, { |
| 167 | + x: -30, |
| 168 | + opacity: 0, |
| 169 | + }), |
| 170 | + gsap.set(this.elements.nameText, { |
| 171 | + text: "", |
| 172 | + }), |
| 173 | + ]; |
| 174 | + |
| 175 | + // Add Tweens to the beginning of the timeline: |
| 176 | + for (const tween of tweens) { |
| 177 | + this.timeline.add(tween, 0); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + // -------------------- Actions -------------------- |
| 182 | + _getUpdateAnimation(payload) { |
| 183 | + const gsap = this.g.gsap; |
| 184 | + |
| 185 | + return [ |
| 186 | + gsap.to(this.elements.nameText, { |
| 187 | + duration: 0.4, |
| 188 | + text: payload.data.name, |
| 189 | + }), |
| 190 | + ]; |
| 191 | + } |
| 192 | + _getPlayAnimation(payload) { |
| 193 | + const gsap = this.g.gsap; |
| 194 | + |
| 195 | + return [ |
| 196 | + gsap.to(this.elements.container, { |
| 197 | + duration: 0.8, |
| 198 | + x: 0, |
| 199 | + opacity: 1, |
| 200 | + ease: "power2.out", |
| 201 | + }), |
| 202 | + |
| 203 | + gsap.to(this.elements.nameText, { |
| 204 | + duration: 0.4, |
| 205 | + // text: this._currentData.name, |
| 206 | + }), |
| 207 | + ]; |
| 208 | + } |
| 209 | + _getStopAnimation(payload) { |
| 210 | + const gsap = this.g.gsap; |
| 211 | + |
| 212 | + return [ |
| 213 | + gsap.to(this.elements.container, { |
| 214 | + duration: 0.8, |
| 215 | + x: -30, |
| 216 | + opacity: 0, |
| 217 | + ease: "power2.in", |
| 218 | + }), |
| 219 | + ]; |
| 220 | + } |
| 221 | + _getHighlightAnimation(payload) { |
| 222 | + const gsap = this.g.gsap; |
| 223 | + |
| 224 | + return [ |
| 225 | + gsap.to(this.elements.container, { |
| 226 | + duration: 0.8, |
| 227 | + backgroundColor: "#ff0", |
| 228 | + color: "#000", |
| 229 | + |
| 230 | + ease: "power2.in", |
| 231 | + }), |
| 232 | + gsap |
| 233 | + .to(this.elements.container, { |
| 234 | + duration: 0.8, |
| 235 | + backgroundColor: "#f00", |
| 236 | + color: "#fff", |
| 237 | + |
| 238 | + ease: "power2.in", |
| 239 | + }) |
| 240 | + .delay(2), |
| 241 | + ]; |
| 242 | + } |
| 243 | +} |
| 244 | + |
| 245 | +export default MyGraphic; |
| 246 | + |
| 247 | +// Note: The renderer will render the component |
0 commit comments