UNPKG

3.02 kBJavaScriptView Raw
1import {
2 watch
3} from "./chunk.W6MGCO4G.js";
4import {
5 emit
6} from "./chunk.UY5AQKHP.js";
7import {
8 e,
9 i,
10 n,
11 t
12} from "./chunk.VKNZYXSO.js";
13import {
14 animated_image_styles_default
15} from "./chunk.H6ATIIQQ.js";
16import {
17 $,
18 s
19} from "./chunk.WWAD5WF4.js";
20import {
21 __decorateClass
22} from "./chunk.K2NRSETB.js";
23
24// src/components/animated-image/animated-image.ts
25var SlAnimatedImage = class extends s {
26 constructor() {
27 super(...arguments);
28 this.isLoaded = false;
29 }
30 handleClick() {
31 this.play = !this.play;
32 }
33 handleLoad() {
34 const canvas = document.createElement("canvas");
35 const { width, height } = this.animatedImage;
36 canvas.width = width;
37 canvas.height = height;
38 canvas.getContext("2d").drawImage(this.animatedImage, 0, 0, width, height);
39 this.frozenFrame = canvas.toDataURL("image/gif");
40 if (!this.isLoaded) {
41 emit(this, "sl-load");
42 this.isLoaded = true;
43 }
44 }
45 handleError() {
46 emit(this, "sl-error");
47 }
48 handlePlayChange() {
49 if (this.play) {
50 this.animatedImage.src = "";
51 this.animatedImage.src = this.src;
52 }
53 }
54 handleSrcChange() {
55 this.isLoaded = false;
56 }
57 render() {
58 return $`
59 <div class="animated-image">
60 <img
61 class="animated-image__animated"
62 src=${this.src}
63 alt=${this.alt}
64 crossorigin="anonymous"
65 aria-hidden=${this.play ? "false" : "true"}
66 @click=${this.handleClick}
67 @load=${this.handleLoad}
68 @error=${this.handleError}
69 />
70
71 ${this.isLoaded ? $`
72 <img
73 class="animated-image__frozen"
74 src=${this.frozenFrame}
75 alt=${this.alt}
76 aria-hidden=${this.play ? "true" : "false"}
77 @click=${this.handleClick}
78 />
79
80 <div part="control-box" class="animated-image__control-box">
81 ${this.play ? $`<sl-icon part="pause-icon" name="pause-fill" library="system"></sl-icon>` : $`<sl-icon part="play-icon" name="play-fill" library="system"></sl-icon>`}
82 </div>
83 ` : ""}
84 </div>
85 `;
86 }
87};
88SlAnimatedImage.styles = animated_image_styles_default;
89__decorateClass([
90 t()
91], SlAnimatedImage.prototype, "frozenFrame", 2);
92__decorateClass([
93 t()
94], SlAnimatedImage.prototype, "isLoaded", 2);
95__decorateClass([
96 i(".animated-image__animated")
97], SlAnimatedImage.prototype, "animatedImage", 2);
98__decorateClass([
99 e()
100], SlAnimatedImage.prototype, "src", 2);
101__decorateClass([
102 e()
103], SlAnimatedImage.prototype, "alt", 2);
104__decorateClass([
105 e({ type: Boolean, reflect: true })
106], SlAnimatedImage.prototype, "play", 2);
107__decorateClass([
108 watch("play", { waitUntilFirstUpdate: true })
109], SlAnimatedImage.prototype, "handlePlayChange", 1);
110__decorateClass([
111 watch("src")
112], SlAnimatedImage.prototype, "handleSrcChange", 1);
113SlAnimatedImage = __decorateClass([
114 n("sl-animated-image")
115], SlAnimatedImage);
116
117export {
118 SlAnimatedImage
119};