forked from kevinpschaaf/sierpinski-triangle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
190 lines (180 loc) · 5.76 KB
/
index.html
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!--
@license
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<!DOCTYPE html>
<html style="width: 100%; height: 100%; overflow: hidden">
<head>
<meta charset="utf-8">
<title>Lit SierpinskiTriangle</title>
<link rel="stylesheet" href="base.css" />
</head>
<body>
<h1>Lit SierpinskiTriangle</h1>
<div id="container">
<example-application></example-application>
</div>
<script type="module">
import {DeferredLitElement as LitElement, html} from './deferred-lit-element.js';
import {styleString} from '@polymer/lit-element/lib/render-helpers.js';
var dotStyle = {
position: 'absolute',
background: '#61dafb',
font: 'normal 15px sans-serif',
textAlign: 'center',
cursor: 'pointer',
};
var containerStyle = {
position: 'absolute',
transformOrigin: '0 0',
left: '50%',
top: '50%',
width: '10px',
height: '10px',
background: '#eee',
};
var targetSize = 25;
class Dot extends LitElement {
static get properties() {
return {
x: {type: Number},
y: {type: Number},
size: {type: Number},
hover: {type: Boolean}
}
}
constructor() {
super();
this.defer = true;
this.hover = false;
}
enter() {
this.hover = true;
}
leave() {
this.hover = false;
}
render() {
var s = this.size * 1.3;
var style = styleString({
...dotStyle,
width: s + 'px',
height: s + 'px',
left: (this.x) + 'px',
top: (this.y) + 'px',
borderRadius: (s / 2) + 'px',
lineHeight: (s) + 'px',
background: this.hover ? '#ff0' : dotStyle.background
});
return html`
<style>
:host {
position: absolute,
background: #61dafb,
font: normal 15px sans-serif,
textAlign: center,
cursor: pointer
}
</style>
<div style="${style}" @mouseover="${() => this.enter()}" @mouseout="${() => this.leave()}">
${this.hover ? '*' : ''}<slot></slot>${this.hover ? '*' : ''}
</div>`;
}
}
customElements.define('s-dot', Dot);
class SierpinskiTriangle extends LitElement {
constructor() {
super();
this.defer = true;
}
static get properties() {
return {
x: {type: Number},
y: {type: Number},
s: {type: Number},
children: {type: Number}
}
}
render() {
let {s, x, y, children} = this;
if (s <= targetSize) {
return html`
<s-dot
x="${x - (targetSize / 2)}"
y="${y - (targetSize / 2)}"
size="${targetSize}"
s-dot>${children}</s-dot>
`;
}
var newSize = s / 2;
var slowDown = true;
if (slowDown) {
var e = performance.now() + 0.8;
while (performance.now() < e) {
// Artificially long execution time.
}
}
s /= 2;
return html`
<s-triangle x="${x}" y="${y - (s / 2)}" s="${s}" children="${children}"></s-triangle>
<s-triangle x="${x - s}" y="${y + (s / 2)}" s="${s}" children="${children}"></s-triangle>
<s-triangle x="${x + s}" y="${y + (s / 2)}" s="${s}" children="${children}"></s-triangle>
`;
}
}
customElements.define('s-triangle', SierpinskiTriangle);
class ExampleApplication extends LitElement {
static get properties() {
return {
elapsed: {type: Number},
seconds: {type: Number}
}
}
constructor() {
super();
this.seconds = 0;
this.tick = this.tick.bind(this);
}
connectedCallback() {
super.connectedCallback();
this.intervalID = setInterval(this.tick, 1000);
}
tick() {
this.seconds = this.seconds % 10 + 1;
}
disconnectedCallback() {
super.disconnectedCallback();
clearInterval(this.intervalID);
}
render() {
const seconds = this.seconds;
const elapsed = this.elapsed;
const t = (elapsed / 1000) % 10;
const scale = 1 + (t > 5 ? 10 - t : t) / 10;
const transform = 'scaleX(' + (scale / 2.1) + ') scaleY(0.7) translateZ(0.1px)';
const style = styleString({ ...containerStyle, transform });
return html`
<div style="${style}">
<div>
<s-triangle x="0" y="0" s="1000" children="${this.seconds}"></s-triangle>
</div>
</div>
`;
}
}
customElements.define('example-application', ExampleApplication);
const start = new Date().getTime();
const app = document.querySelector('example-application');
function update() {
app.elapsed = new Date().getTime() - start;
requestAnimationFrame(update);
}
requestAnimationFrame(update);
</script>
</body>
</html>