-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmandel2.js
258 lines (231 loc) · 6.86 KB
/
mandel2.js
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
PIXI.settings.SCALE_MODE = PIXI.SCALE_MODES.NEAREST;
//PIXI.SCALE_MODES.DEFAULT = PIXI.SCALE_MODES.NEAREST;
window.app = new PIXI.Application({
width: window.innerWidth,
height: window.innerHeight
// resolution: 1
})
document.querySelector('#frame').appendChild(app.view);
const MAX_ITERATIONS=200;
const COLOR_CYCLES=5;
const buttonsSpritesheetData = {
"frames":
{
"plus":
{
"frame": {"x":0,"y":0,"w":8,"h":8},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":8,"h":8},
"sourceSize": {"w":8,"h":8},
"anchor": {"x":0.5,"y":0.5}
},
"minus":
{
"frame": {"x":8,"y":0,"w":8,"h":8},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":8,"h":8},
"sourceSize": {"w":8,"h":8},
"anchor": {"x":0.5,"y":0.5}
},
"up":
{
"frame": {"x":16,"y":0,"w":8,"h":8},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":8,"h":8},
"sourceSize": {"w":8,"h":8},
"anchor": {"x":0.5,"y":0.5}
},
"down":
{
"frame": {"x":24,"y":0,"w":8,"h":8},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":8,"h":8},
"sourceSize": {"w":8,"h":8},
"anchor": {"x":0.5,"y":0.5}
},
"left":
{
"frame": {"x":32,"y":0,"w":8,"h":8},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":8,"h":8},
"sourceSize": {"w":8,"h":8},
"anchor": {"x":0.5,"y":0.5}
},
"right":
{
"frame": {"x":40,"y":0,"w":8,"h":8},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":8,"h":8},
"sourceSize": {"w":8,"h":8},
"anchor": {"x":0.5,"y":0.5}
}
},
"meta":
{
"app": "https://www.codeandweb.com/texturepacker",
"version": "1.0",
"image": "buttons.png",
"format": "RGBA8888",
"size": {"w":48,"h":8},
"scale": "1",
"smartupdate": "$TexturePacker:SmartUpdate:5c89c061071a0c52b3956690ead5a36a:521fbf963877ca6e4aebf698fe8142c2:b34c8670b11bb20cb1751a70d3683e22$"
}
}
fragment = `
precision mediump float;
varying vec2 vTextureCoord;
varying vec4 vColor;
uniform sampler2D uSampler;
uniform float time;
uniform float screenWidth;
uniform float screenHeight;
uniform float screenX;
uniform float screenY;
vec3 hsv2rgb(vec3 c) {
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
void main(void)
{
gl_FragColor = vec4(0, 0, 0, 1.0);
float x = screenX + screenWidth * (vTextureCoord.x - 0.5);
float y = screenY + screenHeight * (vTextureCoord.y - 0.5);
float zx = 0.0;
float zy = 0.0;
float zswap;
float scaled;
float ax;
float ay;
//gl_FragColor = vec4(0.5, 0.5, 0.5, 1.0);
if (abs(x+.18) < .4 && abs(y) < .4) {
return;
}
for(int i=0; i <= ${MAX_ITERATIONS}; i++) {
zswap = zx*zx - zy*zy + x;
zy = (zx+zx)*zy + y;
zx = zswap;
ax = abs(zx);
ay = abs(zy);
if (zx*zx + zy*zy > 4.0) {
scaled=log(float(i))/log(${MAX_ITERATIONS}.);
gl_FragColor = vec4(
hsv2rgb(
vec3(
mod(
scaled,
1./${COLOR_CYCLES}.
)*${COLOR_CYCLES}.,
.2+scaled*1.5, // tops out at 1
scaled*1.5
)
), 1.0
);
return;
}
}
}
`
var screenSize = 2;
const container = new PIXI.Container();
container.filterArea = new PIXI.Rectangle(0, 0, 80, 80);
app.stage.addChild(container);
const filter = new PIXI.Filter(null, fragment, {
// NB: these values get immediately overwritten
screenWidth: screenSize,
screenHeight: screenSize,
screenX: -0.5,
screenY: 0
});
container.filters = [filter];
// app.stage.filterArea = app.renderer.screen;
// app.stage.filters = [filter];
const baseTexture = new PIXI.BaseTexture(buttonsSpritesheetData.meta.image, null, 1);
const spritesheet = new PIXI.Spritesheet(baseTexture, buttonsSpritesheetData);
spritesheet.parse(function (textures) {
// finished preparing spritesheet textures
});
const controls={};
const controlNames=["plus","minus","up","down","left","right"]
for(i=0; i<controlNames.length; i++){
const name = controlNames[i];
controls[name] = new PIXI.Sprite(spritesheet.textures[name]);
app.stage.addChild(controls[name]);
controls[name].interactive = true;
controls[name].buttonMode = true;
controls[name]
.on('pointerdown', function() { controls[name].isDown = true })
.on('pointerup', function() { controls[name].isDown = false })
.on('pointerupoutside', function() { controls[name].isDown = false });
}
const minScreenSize = 70;
centerArrowsAt = function(x, y) {
controls.up.position.set(x,y-8);
controls.down.position.set(x,y+8);
controls.left.position.set(x-8,y);
controls.right.position.set(x+8,y);
}
// Resize function window
resize = function(){
// Get the parent
parent = app.view.parentNode;
// Resize the renderer
app.renderer.resize(window.innerWidth, window.innerHeight);
// Scale the renderer to fit 128x128 plus extra on the bottom or right
var narrowest = Math.min(parent.clientWidth, parent.clientHeight);
var widest = Math.max(parent.clientWidth, parent.clientHeight);
app.stage.scale.set(narrowest / minScreenSize);
const maxScreenSize = minScreenSize * widest / narrowest;
if (parent.clientWidth < parent.clientHeight) {
// portrait
var screenWidth = screenSize;
var screenHeight = screenSize * parent.clientHeight/parent.clientWidth;
controls.plus.position.set(minScreenSize - 8, maxScreenSize - 8);
controls.minus.position.set(minScreenSize - 20, maxScreenSize - 8);
centerArrowsAt(12, maxScreenSize - 12);
} else {
// landscape
var screenWidth = screenSize * parent.clientWidth/parent.clientHeight;
var screenHeight = screenSize;
controls.plus.position.set(maxScreenSize - 8, 8);
controls.minus.position.set(maxScreenSize - 8, 20);
centerArrowsAt(maxScreenSize - 12, minScreenSize - 12);
}
filter.uniforms.screenWidth = screenWidth;
filter.uniforms.screenHeight = screenHeight;
container.filterArea.width=Math.ceil(parent.clientWidth);
container.filterArea.height=Math.ceil(parent.clientHeight);
}
// Listen for window resize events
window.addEventListener('resize', resize);
resize()
const zoomRatio = 1.02;
const panRatio = 0.01;
app.ticker.add(function(deltaT) {
if(controls.plus.isDown) {
screenSize/= zoomRatio;
resize();
}
if(controls.minus.isDown) {
screenSize*= zoomRatio;
resize();
}
if(controls.up.isDown) {
filter.uniforms.screenY-= screenSize*panRatio;
}
if(controls.down.isDown) {
filter.uniforms.screenY+= screenSize*panRatio;
}
if(controls.left.isDown) {
filter.uniforms.screenX-= screenSize*panRatio;
}
if(controls.right.isDown) {
filter.uniforms.screenX+= screenSize*panRatio;
}
});