-
Notifications
You must be signed in to change notification settings - Fork 0
/
sincentro.js
196 lines (156 loc) · 4.83 KB
/
sincentro.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
var pinceles = [];
var tintasFondo = [];
var tintasPincel = [];
var capas = [];
var lienzo;
var estado;
var mensajes;
var presionInicializada = false;
var presion = -2;
var presionEscala = 5;
// --- Subscribers --- //
var subscribePincel = observe(
// selection of the state
function (state) {
return state.pincel;
},
// onChange trigger
function (state) {
if (estado) {
estado.setearPincel(state.value);
}
}
);
// --- DOM Elements actions --- //
// Slider
var sliderInput = document.getElementById("pincel-input");
sliderInput.oninput = function () {
var sliderValue = this.value;
store.dispatch(updatePincel(sliderValue));
};
// Controls switcher
var controlsBTN = document.getElementById("controls-btn");
controlsBTN.onclick = function () {
var controlsContainer = document.querySelector(".controls-container");
controlsContainer.classList.toggle("active");
};
// --- Sketch --- //
var sketch = function (p) {
p.setup = function () {
p.configurarPantallaCompleta();
p.disableScroll();
p.cursor(p.CROSS);
// https://www.codeleaks.io/get-url-parameter-javascript/
// https://developer.mozilla.org/en-US/docs/Web/API/Location/search
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const otroID = urlParams.get('peer')
cargarPinceles(p);
cargarColores(p);
crearCapas(p);
lienzo = new LienzoFondo(p);
estado = new Estado(p);
mensajes = new Mensajes(p);
// iu = new Interface();
// crearInterface();
iniciarP2P(p, otroID);
// mostrarPortada(p);
};
p.draw = function () {
// if (mostrandoPortada()) return
if (!presionInicializada) {
p.inicializarPresion();
}
actualizarEstados();
lienzo.pintar();
pintarCapas();
estado.mostrar();
mensajes.mostrar();
// mostrarInterface();
};
p.mousePressed = function() {
// if (mostrandoID) return
estado.iniciarTrazo(estado.indiceTrazo + 1, p.mouseX, p.mouseY, presion, p.millis(), true);
// return false;
};
p.mouseDragged = function() {
// if (mostrandoID) return
estado.actualizarTrazo(estado.indiceTrazo, p.mouseX, p.mouseY, presion, p.millis(), true);
// return false;
};
p.mouseReleased = function() {
// if (mostrandoID) return
estado.terminarTrazo(estado.indiceTrazo, modificador(p) === p.SHIFT, true);
// return false;
};
p.keyPressed = function() {
// if (mostrandoPortada()) return false
estado.procesarTeclado(p.keyCode, p.key, true);
return false;
};
p.configurarPantallaCompleta = function() {
let w = 0, h = 0;
if (typeof(window.innerWidth) === 'number') {
// Non-IE
w = window.innerWidth;
h = window.innerHeight;
} else if(document.documentElement &&
(document.documentElement.clientWidth ||
document.documentElement.clientHeight)) {
// IE 6+ in 'standards compliant mode'
w = document.documentElement.clientWidth;
h = document.documentElement.clientHeight;
} else if(document.body && (document.body.clientWidth ||
document.body.clientHeight)) {
// IE 4 compatible
w = document.body.clientWidth;
h = document.body.clientHeight;
}
var canvas = p.createCanvas(w, h);
canvas.parent('sincentro');
canvas.id("sincentroCanvas");
canvas.position(0, 0);
};
// Disabling scrolling and bouncing on iOS Safari
// https://stackoverflow.com/questions/7768269/ipad-safari-disable-scrolling-and-bounce-effect
p.preventDefault = function(e) {
e.preventDefault();
};
p.disableScroll = function() {
document.body.addEventListener('touchmove', p.preventDefault, { passive: false });
};
// Initializing Pressure.js
// https://pressurejs.com/documentation.html
p.inicializarPresion = function() {
//console.log("Attempting to initialize Pressure.js ");
Pressure.set('#sincentroCanvas', {
start: function(event){
// this is called on force start
// isDrawing = true;
// isDrawingJustStarted = true;
},
end: function(){
// this is called on force end
// isDrawing = false
presion = 0;
},
change: function(force, event) {
if (!presionInicializada){
console.log("Pressure.js initialized successfully");
presionInicializada = true;
}
//console.log(force);
presion = presionEscala * force;
}
});
Pressure.config({
polyfill: true, // use time-based fallback ?
polyfillSpeedUp: 1000, // how long does the fallback take to reach full pressure
polyfillSpeedDown: 300,
preventSelect: true,
only: null
});
};
};
// --- Punto de entrada del sketch --- //
let sincentro = new p5(sketch, "sincentro");