-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cssSprites.vue
127 lines (117 loc) · 2.24 KB
/
cssSprites.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<template>
<section
class="sprite-demo"
:style="{
'--src': show.sprite.src,
'--columns': show.sprite.columns,
'--rows': show.sprite.rows,
}">
<div
v-for="action in show.actions"
:key="action.name"
:data-action="action.name"
:style="{
'--row': action.row,
}"
/>
</section>
</template>
<script>
const urlSrc = (spriteSrc) => {
if (!spriteSrc.includes('://') && !process.env.isDev) {
spriteSrc = process.env.httpUrl + spriteSrc;
}
return `url(${spriteSrc})`;
}
const monsters = {
sprite: {
src: urlSrc('/images/dynamic-css/vueconf19/animations.png'),
columns: 10,
rows: 5,
},
actions: [
{
name: 'idle',
row: 0,
},
{
name: 'attack',
row: 1,
},
{
name: 'special',
row: 2,
},
],
};
const metroid = {
sprite: {
src: urlSrc('/images/dynamic-css/smashingsf19/metroidsprite.png'),
columns: 10,
rows: 12,
},
actions: [
{
name: 'run-right',
row: 2,
},
{
name: 'run-left',
row: 3,
},
],
};
export default {
data() {
return {
sprites: {
monsters,
metroid,
}
};
},
props: {
sprite: {
type: String,
default: 'monsters'
},
},
computed: {
show() {
return this.sprites[this.sprite];
}
},
}
</script>
<style lang="scss" scoped>
.sprite-demo {
--steps: calc(var(--columns) - 1);
--square: calc(6em + 10vw);
display: grid;
grid-template-columns: repeat(auto-fit, var(--square));
grid-auto-rows: var(--square);
justify-content: center;
justify-items: stretch;
align-items: stretch;
align-content: center;
[data-slide-view='grid'] & {
--square: calc(5em + 5vw);
}
}
@keyframes sprite {
to {
background-position: 100% var(--posY);
}
}
[data-action] {
--posY: calc(0px - var(--square) * var(--row));
animation: sprite 1s steps(var(--steps), end) infinite paused;
background-image: var(--src);
background-size: calc(var(--square) * var(--columns)) auto;
background-position: 0% var(--posY);
width: 100%;
&:hover {
animation-play-state: running;
}
}
</style>