-
Notifications
You must be signed in to change notification settings - Fork 3
/
sketch4.js
199 lines (178 loc) · 5.75 KB
/
sketch4.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
let fractalDiv;
let divWidth, divHeight
let length, proportion, maxlevel, colorPicker, uploadedImage, logo_im, logo_unam, marco;
let canvas;
function setup() {
//Contenedor
fractalDiv = createDiv();
fractalDiv.parent("fractal");
fractalDiv.class("fractalSkin")
//Asignacion de tamaños apartir del div
divWidth = fractalDiv.size().width;
divHeight = fractalDiv.size().height;
//Creacion canvas
canvas = createCanvas(divWidth, windowHeight / 1.4);
// colocar canvas en contenedor
canvas.parent(fractalDiv);
//Slider Iteraciones
let text1 = createP('Iteraciones');
text1.parent("slider");
iterationsSlider = createSlider(1, 6, 3, 1);
iterationsSlider.parent("slider");
iterationsSlider.class("sliderSkin");
//Slider Proporcion
let text2 = createP('Proporción');
text2.parent("slider");
proportionSlider = createSlider(0.2, 0.8, 0.4, 0.01);
proportionSlider.parent("slider");
proportionSlider.class("sliderSkin");
//Slider Zoom
let text3 = createP('zoom');
text3.parent("slider");
lengthSlider = createSlider(divWidth / 10, divWidth / 2, divWidth / 5, 0);
lengthSlider.parent("slider");
lengthSlider.class("sliderSkin");
//ColorPicker del fondo
let texto_color = createP("Color del fondo");
texto_color.parent("slider");
colorPicker = createColorPicker(color(173, 216, 230));
colorPicker.parent("slider");
colorPicker.class("sliderSkin");
//ColorPicker del fractal
let cuadro_color = createP("Color del fractal");
cuadro_color.parent("slider");
colorPickerCuadro = createColorPicker(color(random(0, 255), random(0, 255), random(0, 255)));
colorPickerCuadro.parent("slider");
colorPickerCuadro.class("sliderSkin");
//Deteccion de cambios
iterationsSlider.changed(drawFractal);
proportionSlider.changed(drawFractal);
lengthSlider.changed(drawFractal);
colorPicker.changed(drawFractal);
// Evento para cambiar el colorPicker
colorPicker.input(changeBackgroundColor);
}
function draw() {
clear()
translate(width / 2, height / 2);
//translate(divWidth / 2,height/2);
//translate(canvas.width / 2,canvas.height/2);
drawFractal();
//image(logo_im, -canvas.width/2+5, -canvas.height/2 , canvas.width/12, canvas.width/12);
//image(logo_unam, -canvas.width/2 +10, -canvas.height/2, canvas.width/13, canvas.width/13);
}
function drawFractal() {
maxlevel = iterationsSlider.value();
proportion = proportionSlider.value();
length = lengthSlider.value();
h = length / 2;
f_1 = funciones(createVector(h, -h));
g_1 = funciones(createVector(h, h));
f_2 = funciones(createVector(-h, h));
g_2 = funciones(createVector(-h, -h));
transformaciones = [f_1, g_1, f_2, g_2];
drawOrbit(transformaciones, createVector(-h, -h));
}
function funciones(direccion) {
return function (p) {
return p5.Vector.add(
p5.Vector.mult(
p5.Vector.add(p, direccion), proportion), direccion);
}
}
function createIndexesNotInverse() {
let inotinverse = [];
let kinv;
for (let k = 1; k <= 4; k++) {
kinv = ((k + 1) % 4);
let a = [];
let b = [];
for (let j = kinv + 1; j < 4; j++) {
append(a, j); 250
}
for (let j = 0; j <= kinv - 1; j++) {
append(b, j);
}
c = a.concat(b);
inotinverse.push(c);
}
return inotinverse;
}
function drawOrbit(transformaciones, p) {
indexesNotInverse = createIndexesNotInverse();
function traverseForward(p, index, level) {
if (uploadedImage) {
image(uploadedImage, p.x, p.y, length * (proportion ** (level)), length * (proportion ** (level)));
} else {
fill(colorPickerCuadro.value());
square(p.x, p.y, length * (proportion ** (level)));
}
if (level >= maxlevel) {
return;
}
let indices = indexesNotInverse[index];
for (let i = 0; i < 3; i++) {
traverseForward(transformaciones[indices[i]](p), indices[i], level + 1);
}
}
if (uploadedImage) {
image(uploadedImage, p.x, p.y, length, length);
} else {
fill(colorPickerCuadro.value())
square(p.x, p.y, length);
}
for (let i = 0; i < 4; i++) {
traverseForward(transformaciones[i](p), i, 1);
}
}
function preload() {
//logo_im = loadImage("imgs/im.png");
//logo_unam = loadImage("imgs/unam.png");
//marco = loadImage("marco.png");
}
function windowResized() {
// Actualiza el tamaño del lienzo cuando se cambia el tamaño de la ventana
divWidth = fractalDiv.size().width;
resizeCanvas(divWidth, windowHeight / 1.4);
}
// Cambiar color background
function changeBackgroundColor() {
// Obtener el color seleccionado por el color picker
let selectedColor = colorPicker.color();
// Aplicar el color como fondo del contenedor fractalDiv
fractalDiv.style("background-color", selectedColor.toString());
}
function uploadImage() {
const fileInput = document.getElementById('imageUpload');
const file = fileInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (event) {
uploadedImage = loadImage(event.target.result, function (img) {
console.log("Ya se subió");
drawFractal();
//para que se actualice el fractal justo después de subir la foto
});
};
reader.readAsDataURL(file);
} else {
console.error("No hay foto");
}
}
//Funcion para descargar la imagen
function descargarImagen() {
//Medidas recorte y centro
let x = (canvas.width - canvas.height) / 2;
let y = 0;
let w = canvas.width;
let h = canvas.height;
//Creacion del canvas cuadrado
let imagenDescargar = createGraphics(h, h);
//Asignacion de color al fondo del canvas para que se vea en la descarga
imagenDescargar.background(colorPicker.color())
// Aqui se podra agregar un marco. //
//Recorte del canvas
imagenDescargar.image(canvas.get(x, y, w, h), 0, 0);
//Descarga del nuevo canvas
imagenDescargar.save("Fractal.png");
}