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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
| function Point2d(x, y) {
"use strict";
Object.defineProperty(this, 'x', {
value: x,
writable: false,
enumerable: true,
configurable: false
});
Object.defineProperty(this, 'y', {
value: y,
writable: false,
enumerable: true,
configurable: false
});
}
Point2d.prototype.horizontalSymmetry = function (n) {
"use strict";
const x = this.x;
const y = this.y - 2 * (this.y - n);
return new Point2d(x, y);
};
Point2d.prototype.verticalSymmetry = function (n) {
"use strict";
const x = this.x + 2 * (n - this.x);
const y = this.y;
return new Point2d(x, y);
};
Point2d.prototype.average = function (p, alpha) {
"use strict";
const x = Math.floor(p.x * alpha + this.x * (1 - alpha));
const y = Math.floor(p.y * alpha + this.y * (1 - alpha));
return new Point2d(x, y);
};
Point2d.prototype.clone = function (p) {
"use strict";
return new Point2d(p.x, p.y);
};
function Segment(p1, p2, couleur = 'black') {
Object.defineProperty(this, 'p1', {
value: p1,
writable: false,
enumerable: true,
configurable: false
});
Object.defineProperty(this, 'p2', {
value: p2,
writable: false,
enumerable: true,
configurable: false
});
Object.defineProperty(this, 'couleur', {
value: couleur,
writable: false,
enumerable: true,
configurable: false
});
}
Segment.prototype.horizontalSymmetry = function (n) {
"use strict";
return new Segment(this.p1.horizontalSymmetry(n), this.p2.horizontalSymmetry(n), this.couleur);
};
Segment.prototype.verticalSymmetry = function (n) {
"use strict";
return new Segment(this.p1.verticalSymmetry(n), this.p2.verticalSymmetry(n), this.couleur);
};
Segment.prototype.average = function (p, alpha) {
"use strict";
return new Segment(this.p1.average(p.p1, alpha), this.p2.average(p.p2, alpha), 'black');
};
//Pour stocker les segments dans un tableau
function Model(ctx, canvas, couleur_jeu, val = []) {
this.tableau_de_segments = val;
this.ctx = ctx;
this.canvas = canvas;
this.couleur_jeu = couleur_jeu;
this.p1 = "no";
this.point_temporaire = null;
}
Model.prototype.ajouter_segment = function (p1) {
"use strict";
if (this.p1 === "yes") {
this.tableau_de_segments.push(new Segment(this.point_temporaire, p1));
this.p1 = "no";
}
if (this.p1 === "no") {
this.point_temporaire = p1;
this.p1 = "yes";
}
console.log(JSON.stringify(this.tableau_de_segments));
};
Model.prototype.paint = function (couleur_jeu) {
"use strict";
var i = this.tableau_de_segments.length,j;
for (j = 0; j < i; j+= 1) {
dessinertrait(this.ctx, this.tableau_de_segments[j].p1.x, this.tableau_de_segments[j].p1.y, this.tableau_de_segments[j].p2.x, this.tableau_de_segments[j].p2.y, this.couleur_jeu.couleur, this.couleur_jeu.epaisseur);
}
interface_depart(this.ctx);
};
Model.prototype.symetrie = function (p) {
"use strict";
if (this.p1 === "yes") {
this.point_temporaire = this.point_temporaire.verticalSymmetry(p);
}
var i = this.tableau_de_segments.length,
j;
for (j = 0; j < i; j+= 1) {
this.tableau_de_segments[j] = this.tableau_de_segments[j].verticalSymmetry(p);
}
};
Model.prototype.se_vider = function () {
"use strict";
this.tableau_de_segments = [];
this.p1 = "no";
this.point_temporaire = null;
};
//POUR AFFICHER LES DIFFERENTS SEGMENTS
function dessinertrait(ctx, debutx, debuty, finx, finy, couleur, ep) {
"use strict";
ctx.beginPath();
ctx.strokeStyle = couleur;
ctx.lineWidth = ep;
ctx.moveTo(debutx, debuty);
ctx.lineTo(finx, finy);
ctx.stroke();
}
//dessiner
function interface_depart(ctx) {
"use strict";
ctx.beginPath();
ctx.strokeStyle = "black";
ctx.lineWidth = "5";
ctx.rect(0, 0, 400, 400);
ctx.stroke();
}
function nouvelle(liste_model) {
"use strict";
liste_model.p1 = "no";
liste_model.point_temporaire = null;
}
function annuler(liste_model) {
"use strict";
if (liste_model.tableau_de_segments.length <= 1) {
liste_model.se_vider();
} else {
liste_model.tableau_de_segments.pop();
var i = liste_model.tableau_de_segments.length;
liste_model.p1 = "yes";
liste_model.point_temporaire = liste_model.tableau_de_segments[i - 1].p2;
}
effacer_ecran(liste_model.ctx);
liste_model.paint();
}
function symetries(liste_model) {
"use strict";
liste_model.symetrie(200);
effacer_ecran(liste_model.ctx);
liste_model.paint();
}
function clear(liste_model) {
"use strict";
liste_model.se_vider();
effacer_ecran(liste_model.ctx);
interface_depart(liste_model.ctx);
}
function attribution_couleur(liste_model) {
"use strict";
switch (document.getElementById("couleur").value) {
case "Noir":
liste_model.couleur_jeu.couleur = "#000000";
break;
case "Vert":
liste_model.couleur_jeu.couleur = "#00FF00";
break;
case "Rouge":
liste_model.couleur_jeu.couleur = "#FF0000";
break;
case "Bleu":
liste_model.couleur_jeu.couleur = "#0000FF";
break;
case "Marron":
liste_model.couleur_jeu.couleur = "#61210B";
break;
default:
liste_model.couleur_jeu.couleur = "#000000";
}
}
function valider(liste_model) {
"use strict";
window.top.window.scrollTo(0, 0);
attribution_couleur(liste_model);
liste_model.couleur_jeu.epaisseur = document.getElementById("epaisseur1").value;
effacer_ecran(liste_model.ctx);
liste_model.paint();
interface_depart(liste_model.ctx);
}
function valider1(liste_model, liste_model1) {
"use strict";
window.top.window.scrollTo(0, 0);
attribution_couleur(liste_model);
liste_model.couleur_jeu.epaisseur = document.getElementById("epaisseur1").value;
effacer_ecran(liste_model.ctx);
liste_model.paint();
effacer_ecran1(liste_model1.ctx);
liste_model1.paint();
interface_depart(liste_model.ctx);
}
function valider_dessin(liste_model1, liste_model, liste_model2_1, liste_model2_2) {
"use strict";
notice1(liste_model1.ctx);
window.setTimeout(function () {
mise_en_forme(liste_model1);
}, 5000);
document.getElementById("calque_valider_dessin1").style.display = "block";
document.getElementById("calque_valider_dessin").style.display = "none";
liste_model.canvas.onclick = function (ev) {
vide();
};
effacer_ecran1(liste_model.ctx);
liste_model.paint();
attribution_bouton(liste_model1, liste_model, liste_model2_1, liste_model2_2);
}
function effacer_ecran(context) {
"use strict";
context.beginPath();
context.rect(0, 0, 400, 400);
context.fillStyle = "white";
context.fill();
context.stroke();
}
function effacer_ecran1(context) {
"use strict";
context.beginPath();
context.rect(0, 0, 400, 400);
context.fillStyle = "#E6E6E6";
context.fill();
context.stroke();
}
function notice(ctx) {
"use strict";
ctx.font = '23px serif';
ctx.textBaseline = 'hanging';
ctx.fillText('Bienvenue sur thalassa !', 80, 180);
ctx.font = '18px serif';
ctx.fillText('Dessiner votre premier dessin :', 80, 219);
ctx.font = '15px serif';
ctx.font = '15px serif';
ctx.fillText('-> Pour dessiner une polyligne utiliser le clique gauche:', 19, 240);
ctx.fillText('-> Pour dessiner une nouvelle polyligne cliquer sur nouvelle ligne:', 0, 257);
}
function notice1(ctx) {
"use strict";
ctx.font = '23px serif';
ctx.textBaseline = 'hanging';
ctx.fillText('Dessiner votre deuxieme dessin :', 60, 170);
ctx.font = '15px serif';
ctx.font = '15px serif';
ctx.fillText('-> Pour dessiner une polyligne utiliser le clique gauche:', 19, 210);
ctx.fillText('-> Pour dessiner une nouvelle polyligne cliquer sur nouvelle ligne:', 0, 230);
}
function mise_en_forme(liste_model) {
"use strict";
effacer_ecran(liste_model.ctx);
liste_model.paint();
}
function attribution_bouton(liste_model, liste_model1, liste_model2_1, liste_model2_2) {
"use strict";
liste_model.canvas.onclick = function (ev) {
position_clique_point(ev, liste_model);
};
var bouton_ligne = document.getElementById('nouvelle_ligne');
bouton_ligne.onclick = function () {
nouvelle(liste_model);
};
var bouton_annuler = document.getElementById('annuler');
bouton_annuler.onclick = function () {
annuler(liste_model);
};
var bouton_symetries = document.getElementById('symetries');
bouton_symetries.onclick = function () {
symetries(liste_model);
};
var bouton_clear = document.getElementById('clear');
bouton_clear.onclick = function () {
clear(liste_model);
};
var bouton_valider = document.getElementById('valider');
bouton_valider.onclick = function () {
valider1(liste_model, liste_model1);
};
var bouton_valider_dessin = document.getElementById('valider_dessin');
bouton_valider_dessin.onclick = function () {
valider_dessin(liste_model1, liste_model, liste_model2_1, liste_model2_2);
};
var bouton_recommencer = document.getElementById('recommencer');
bouton_recommencer.onclick = function () {
document.location.reload(true);
};
var bouton_valider_dessin1 = document.getElementById('valider_dessin1');
bouton_valider_dessin1.onclick = function () {
afficher_animation(liste_model1, liste_model, liste_model2_1, liste_model2_2);
};
bouton_valider_dessin1.style.display = "block";
}
function afficher_animation(liste_model1, liste_model, liste_model2_1, liste_model2_2) {
"use strict";
interpolation_de_forme(liste_model1, liste_model, liste_model2_1, liste_model2_2, 0);
}
function interpolation_de_forme(liste_model1, liste_model, liste_model2_1, liste_model2_2, alpha) {
"use strict";
var t1 = liste_model1.tableau_de_segments.length;
var t2 = liste_model.tableau_de_segments.length;
var boucle = true;
var i = 0,
j = 0;
liste_model2_1.se_vider();
liste_model2_2.se_vider();
while (boucle) {
liste_model2_1.tableau_de_segments.push(liste_model1.tableau_de_segments[i].average(liste_model.tableau_de_segments[j], alpha));
liste_model2_2.tableau_de_segments.push(liste_model1.tableau_de_segments[i].average(liste_model.tableau_de_segments[j], 1 - alpha));
if (t1 > t2) {
i = (i + 1);
j = (j + 1) % t2;
if (i === t1) {
boucle = false;
}
} else {
i = (i + 1) % t1;
j = (j + 1);
if (j === t2) {
boucle = false;
}
}
}
if (alpha <= 1) {
effacer_ecran(liste_model2_1.ctx);
effacer_ecran(liste_model2_2.ctx);
liste_model2_1.paint();
liste_model2_2.paint();
window.setTimeout(function () {
interpolation_de_forme(liste_model1, liste_model, liste_model2_1, liste_model2_2, alpha + 0.01);
}, 70);
}
}
function position_clique_point(ev, liste_model) {
"use strict";
liste_model.ajouter_segment(new Point2d(ev.clientX - liste_model.canvas.offsetLeft, ev.clientY - liste_model.canvas.offsetTop));
effacer_ecran(liste_model.ctx);
liste_model.paint();
}
function vide() {
"use strict";
}
function lancement() {
"use strict";
var liste_des_points = [];
var couleur_jeu = new couleur('black', 5);
var canvas = document.getElementById('jeu');
var ctx = canvas.getContext("2d");
var canvas1 = document.getElementById('jeu1');
var ctx1 = canvas1.getContext("2d");
var liste_model = new Model(ctx, canvas, couleur_jeu);
var liste_model1 = new Model(ctx1, canvas1, couleur_jeu);
var liste_model2_1 = new Model(ctx, canvas, couleur_jeu);
var liste_model2_2 = new Model(ctx1, canvas1, couleur_jeu);
interface_depart(ctx);
interface_depart(ctx1);
notice(ctx);
window.setTimeout(function () {
mise_en_forme(liste_model);
}, 5000);
attribution_bouton(liste_model, liste_model1, liste_model2_1, liste_model2_2);
}
window.onload = lancement; |
Partager