a1ec07dae303a928e33284ff46bc27ac65b817d4
[Portfolio.git] / skins / photo_film_viewer.js
1 /*
2 * © 2008-2014 Benoît Pin – Centre de recherche en informatique – MINES ParisTech
3 * http://plinn.org
4 * Licence Creative Commons http://creativecommons.org/licenses/by-nc/2.0/
5 *
6 *
7 */
8
9 var FilmSlider;
10
11 (function(){
12
13 var keyLeft = 37, keyRight = 39;
14 var isTextMime = /^text\/.+/i;
15 var isAddToSelection = /.*\/add_to_selection$/;
16 var imgRequestedSize = /size=(\d+)/;
17 var DEFAULT_IMAGE_SIZES = [500, 600, 800];
18
19 FilmSlider = function(filmBar, slider, ctxInfos, image, toolbar, breadcrumbs) {
20 var thisSlider = this;
21 this.filmBar = filmBar;
22 var film = filmBar.firstChild;
23 if (film.nodeType === 3) { film = film.nextSibling; }
24 this.film = film;
25 this.slider = slider;
26 this.rail = slider.parentNode;
27 this.sliderSpeedRatio = undefined;
28 this.sliderRatio = undefined;
29 this.selectedSlide = undefined;
30 this.selectedSlideInSelection = undefined;
31 this.cartSlide = document.getElementById('cart_slide');
32 this.image = image;
33 this.stretchable = image.parentNode;
34 this.viewMode = 'medium';
35
36 this.buttons = [];
37 this.toolbar = toolbar;
38 if (breadcrumbs) {
39 var bcElements = breadcrumbs.getElementsByTagName('a');
40 this.lastBCElement = bcElements[bcElements.length-1];
41 var imgSrcParts = image.src.split('/');
42 this.lastBCElement.innerHTML = imgSrcParts[imgSrcParts.length-2];
43 this.hasBreadcrumbs = true;
44 }
45 else {
46 this.hasBreadcrumbs = false;
47 }
48
49 var buttons = toolbar.getElementsByTagName('img');
50 var b, name, i;
51 for (i=0 ; i<buttons.length ; i++) {
52 b = buttons[i];
53 name = b.getAttribute('name');
54 if (name) { this.buttons[name] = b; }
55 }
56
57 this.pendingImage = new Image();
58 this.pendingImage.onload = function() {
59 thisSlider.refreshImage();
60 };
61 this.initialized = false;
62
63 this.film.style.left='0';
64 this.film.style.top='0';
65
66 this.filmLength = ctxInfos.filmLength;
67 this.center = ctxInfos.center;
68 this.slideSize = ctxInfos.slideSize;
69 this.ctxUrlTranslation = ctxInfos.ctxUrlTranslation;
70
71 this.ddHandlers = {'down' : function(evt){thisSlider.mouseDownHandler(evt);},
72 'move' : function(evt){thisSlider.mouseMoveHandler(evt);},
73 'up' : function(evt){thisSlider.mouseUpHandler(evt);},
74 'out' : function(evt){thisSlider.mouseOutHandler(evt);}
75 };
76
77 this.resizeSlider();
78 this.addEventListeners();
79 };
80
81
82 FilmSlider.prototype.resizeSlider = function(evt) {
83 var filmBarWidth = getObjectWidth(this.filmBar);
84 if (!filmBarWidth) {
85 var thisSlider = this;
86 addListener(window, 'load', function(evt){thisSlider.resizeSlider(evt);});
87 return;
88 }
89
90 var filmWidth = this.slideSize * this.filmLength;
91 var sliderRatio = this.sliderRatio = filmBarWidth / filmWidth;
92 var sliderWidth = filmBarWidth * sliderRatio;
93 this.rail.style.width = filmBarWidth + 'px';
94 this.rail.style.display = 'block';
95 this.rail.style.visibility = 'visible';
96 if (sliderRatio < 1) {
97 this.slider.style.width = Math.round(sliderWidth) + 'px';
98 this.slider.style.visibility = 'visible';
99 }
100 else {
101 this.slider.style.visibility = 'hidden';
102 }
103
104 this.winSize = {'width' : getWindowWidth(),
105 'height' : getWindowHeight()};
106 this.maxRightPosition = filmBarWidth - sliderWidth;
107 this.sliderSpeedRatio = - (filmBarWidth - sliderWidth) / (filmWidth - filmBarWidth);
108 if (!this.initialized) {
109 this.centerSlide(this.center);
110 this.selectedSlide = this.filmBar.getElementsByTagName('img')[this.center].parentNode;
111 this.initialized = true;
112 }
113 };
114
115 FilmSlider.prototype.fitToScreen = function(evt) {
116 this._fitToScreen();
117 var thisSlider = this;
118 addListener(window, 'resize', function(evt){thisSlider._fitToScreen();});
119 };
120
121 FilmSlider.prototype._fitToScreen = function(evt) {
122 var wh = getWindowHeight();
123 var rb = getObjectTop(this.rail) + getObjectHeight(this.rail); // rail bottom
124 var delta = wh - rb;
125 var sh = getObjectHeight(this.stretchable);
126 var newSize = sh + delta;
127 this.stretchable.style.height = newSize + 'px';
128
129 var ratio = this.image.height / this.image.width;
130 var bestFitSize = this.getBestFitSize(ratio);
131 var currentSize = parseInt(imgRequestedSize.exec(this.image.src)[1], 10);
132 if (currentSize !== bestFitSize) {
133 var src = this.image.src.replace(imgRequestedSize, 'size=' + bestFitSize);
134 this.pendingImage.src = src;
135 }
136 };
137
138 FilmSlider.prototype.getBestFitSize = function(ratio) {
139 var fw = getObjectWidth(this.stretchable) - 1;
140 var fh = getObjectHeight(this.stretchable) - 1;
141
142 var i, irw, irh;
143 if (ratio < 1) {
144 for (i=DEFAULT_IMAGE_SIZES.length -1 ; i>0 ; i--) {
145 irw = DEFAULT_IMAGE_SIZES[i];
146 irh = irw * ratio;
147 if (irw <= fw && irh <= fh) { break; }
148 }
149 }
150 else {
151 for (i=DEFAULT_IMAGE_SIZES.length -1 ; i>0 ; i--) {
152 irh = DEFAULT_IMAGE_SIZES[i];
153 irw = irh / ratio;
154 if (irw <= fw && irh <= fh) { break; }
155 }
156 }
157 return DEFAULT_IMAGE_SIZES[i];
158 };
159
160 FilmSlider.prototype.centerSlide = function(slideIndex) {
161 if (this.sliderRatio > 1) { return; }
162 var filmBarWidth = getObjectWidth(this.filmBar);
163 var x = slideIndex * this.slideSize;
164 x = x - (filmBarWidth - this.slideSize) / 2.0;
165 x = x * this.sliderSpeedRatio;
166 var p = new Point( -x, 0 );
167 this.setSliderPosition(p);
168 };
169
170 FilmSlider.prototype.setSliderPosition = function(point) {
171 if(point.x < 0) { point.x = 0; }
172 if (point.x > this.maxRightPosition) { point.x = this.maxRightPosition; }
173 this.slider.style.left = point.x + 'px';
174 this.setFilmPosition(point);
175 };
176
177 FilmSlider.prototype.setFilmPosition = function(point) {
178 this.film.style.left = point.x / this.sliderSpeedRatio + 'px';
179 };
180
181 FilmSlider.prototype.getSliderPosition = function() {
182 var x = parseInt(this.slider.style.left, 10);
183 var y = parseInt(this.slider.style.top, 10);
184 var p = new Point(x, y);
185 return p;
186 };
187
188 FilmSlider.prototype.getFilmPosition = function() {
189 var x = parseInt(this.film.style.left, 10);
190 var y = parseInt(this.film.style.top, 10);
191 var p = new Point(x, y);
192 return p;
193 };
194
195 FilmSlider.prototype.loadSibling = function(previous) {
196 var slide = null;
197 if (previous) {
198 slide = this.selectedSlide.parentNode.previousSibling;
199 if (slide && slide.nodeType===3) { slide = slide.previousSibling; }
200 }
201 else {
202 slide = this.selectedSlide.parentNode.nextSibling;
203 if (slide && slide.nodeType===3) { slide = slide.nextSibling; }
204 }
205
206 if (!slide) { return; }
207 else {
208 var target = slide.getElementsByTagName('a')[0];
209 raiseMouseEvent(target, 'click');
210 var index = parseInt(target.getAttribute('portfolio:position'), 10);
211 this.centerSlide(index);
212 }
213 };
214
215 FilmSlider.prototype.addEventListeners = function() {
216 var thisSlider = this;
217 addListener(window, 'resize', function(evt){thisSlider.resizeSlider(evt);});
218 addListener(this.filmBar, 'click', function(evt){thisSlider.thumbnailClickHandler(evt);});
219 addListener(this.toolbar, 'click', function(evt){thisSlider.toolbarClickHandler(evt);});
220 addListener(window, 'load', function(evt){thisSlider.fitToScreen(evt);});
221
222 // dd listeners
223 addListener(this.slider, 'mousedown', this.ddHandlers.down);
224 if(browser.isDOM2Event){
225 if (browser.isAppleWebKit) {
226 this.filmBar.addEventListener('mousewheel', function(evt){thisSlider.mouseWheelHandler(evt);}, false);
227 }
228 else {
229 addListener(this.filmBar, 'DOMMouseScroll', function(evt){thisSlider.mouseWheelHandler(evt);});
230 }
231 }
232 else if (browser.isIE6up) {
233 addListener(this.filmBar, 'mousewheel', function(evt){thisSlider.mouseWheelHandler(evt);});
234 }
235
236 addListener(document, 'keydown', function(evt){thisSlider.keyDownHandler(evt);});
237 addListener(document, 'keypress', function(evt){thisSlider.keyPressHandler(evt);});
238 };
239
240
241 FilmSlider.prototype.mouseDownHandler = function(evt) {
242 this.initialClickPoint = new Point(evt.clientX, evt.clientY);
243 this.initialPosition = this.getSliderPosition();
244 this.dragInProgress = true;
245 addListener(document, 'mousemove', this.ddHandlers.move);
246 addListener(document, 'mouseup', this.ddHandlers.up);
247 addListener(document.body, 'mouseout', this.ddHandlers.out);
248 };
249
250
251 FilmSlider.prototype.mouseMoveHandler = function(evt) {
252 if(!this.dragInProgress) { return; }
253
254 clearSelection();
255 evt = getEventObject(evt);
256 var currentPoint = new Point(evt.clientX, evt.clientY);
257 var displacement = currentPoint.diff(this.initialClickPoint);
258 this.setSliderPosition(this.initialPosition.add(displacement));
259 };
260
261 FilmSlider.prototype.mouseUpHandler = function(evt) {
262 this.dragInProgress = false;
263 evt = getEventObject(evt);
264 this.mouseMoveHandler(evt);
265 };
266
267
268 FilmSlider.prototype.mouseOutHandler = function(evt) {
269 evt = getEventObject(evt);
270 var x = evt.clientX;
271 var y = evt.clientY;
272 if (x < 0 ||
273 x > this.winSize.width ||
274 y < 0 ||
275 y > this.winSize.height
276 ) {
277 this.mouseUpHandler(evt);
278 }
279 };
280
281 FilmSlider.prototype.thumbnailClickHandler = function(evt) {
282 var target = getTargetedObject(evt);
283 while (target.tagName !== 'A' && target !== this.filmBar) { target = target.parentNode; }
284 if (target.tagName !== 'A') { return; }
285 else {
286 if (this.viewMode === 'full') {
287 this.mosaique.unload();
288 this.mosaique = null;
289 this.viewMode = 'medium';
290 }
291 disableDefault(evt);
292 disablePropagation(evt);
293 target.blur();
294 history.pushState(target.href, '', target.href);
295
296 var imgBaseUrl = target.href;
297 var canonicalImgUrl;
298 if (this.ctxUrlTranslation[0]) {
299 canonicalImgUrl = imgBaseUrl.replace(this.ctxUrlTranslation[0],
300 this.ctxUrlTranslation[1]);
301 }
302 else { canonicalImgUrl = imgBaseUrl; }
303
304 var ajaxUrl = imgBaseUrl + '/photo_view_ajax';
305 var thisFS = this;
306
307 //this.pendingImage.src = canonicalImgUrl + '/getResizedImage?size=600';
308 var thumbnail = target.getElementsByTagName('IMG')[0];
309 var bestFitSize = this.getBestFitSize(thumbnail.height/thumbnail.width);
310 this.pendingImage.src = canonicalImgUrl + '/getResizedImage?size=' + bestFitSize;
311
312 // update buttons
313 var fullScreenLink = this.buttons.full_screen.parentNode;
314 fullScreenLink.href = canonicalImgUrl + '/zoom_view';
315
316 var toggleSelectionBtn = this.buttons.toggle_selection;
317 var toggleSelectionLink = toggleSelectionBtn.parentNode;
318 this.selectedSlideInSelection = (target.className==='selected');
319 if (this.selectedSlideInSelection) {
320 toggleSelectionBtn.src = portal_url() + '/unselect_flag_btn.gif';
321 toggleSelectionBtn.alt = toggleSelectionLink.title = 'Retirer de la sélection';
322 toggleSelectionLink.href = canonicalImgUrl + '/remove_to_selection';
323 }
324 else {
325 toggleSelectionBtn.src = portal_url() + '/select_flag_btn.gif';
326 toggleSelectionBtn.alt = toggleSelectionLink.title = 'Ajouter à la sélection';
327 toggleSelectionLink.href = canonicalImgUrl + '/add_to_selection';
328 }
329
330 var showBuyableButtonLink = this.buttons.show_buyable.parentNode;
331 showBuyableButtonLink.href = canonicalImgUrl + '/get_slide_buyable_items';
332 this.cartSlide.innerHTML = '';
333 this.cartSlide.style.visibility='hidden';
334
335
336 var metadataButton = this.buttons.edit_metadata
337 if (metadataButton) {
338 var metadataEditLink = metadataButton.parentNode;
339 metadataEditLink.href = canonicalImgUrl + '/photo_edit_form'
340 }
341
342
343 var req = new XMLHttpRequest();
344 req.onreadystatechange = function() {
345 switch (req.readyState) {
346 case 1 :
347 showProgressImage();
348 break;
349 case 2 :
350 try {
351 if (! isTextMime.exec(req.getResponseHeader('Content-Type'))) {
352 req.onreadystatechange = null;
353 req.abort();
354 hideProgressImage();
355 window.location.href = thisFS._fallBackUrl;
356 }
357 }
358 catch(e){}
359 break;
360 case 4 :
361 hideProgressImage();
362 if (req.status === '200') { thisFS.populateViewer(req); }
363 // else
364 // //window.location.href = target.href;
365 // console.error(ajaxUrl);
366
367 };
368 };
369
370 req.open("GET", ajaxUrl, true);
371 req.send(null);
372
373 // update old displayed slide className
374 var className = this.selectedSlide.className;
375 var classes = className.split(' ');
376 var newClasses = [];
377 var name, i;
378
379 for (i in classes) {
380 name = classes[i];
381 if (name === 'displayed') { continue; }
382 else { newClasses.push(name); }
383 }
384
385 this.selectedSlide.className = newClasses.join(' ')
386
387 // hightlight new displayed slide
388 this.selectedSlide = target;
389 className = this.selectedSlide.className;
390 classes = className.split(' ');
391 classes.push('displayed');
392 this.selectedSlide.className = classes.join(' ');
393 }
394 };
395
396 FilmSlider.prototype.toolbarClickHandler = function(evt) {
397 var target = getTargetedObject(evt);
398 if(target.tagName === 'IMG' && target.getAttribute('name')) {
399 switch(target.getAttribute('name')) {
400 case 'previous' :
401 disableDefault(evt);
402 disablePropagation(evt);
403 var button = target;
404 var link = button.parentNode;
405 link.blur();
406 this.loadSibling(true);
407 break;
408 case 'next' :
409 disableDefault(evt);
410 disablePropagation(evt);
411 var button = target;
412 var link = button.parentNode;
413 link.blur();
414 this.loadSibling(false);
415 break;
416 case 'full_screen':
417 disableDefault(evt);
418 disablePropagation(evt);
419 target.parentNode.blur();
420 if (this.viewMode === 'full') {
421 this.mosaique.unload();
422 this.mosaique = null;
423 this.viewMode = 'medium';
424 return;
425 }
426 var main = document.getElementById('photo_viewer');
427 var url = target.parentNode.href;
428 url = url.substring(0, url.length - '/zoom_view'.length);
429 var margins = {'top':0, 'right':-1, 'bottom':0, 'left':0};
430 this.mosaique = new Mosaique(main, url, margins);
431 this.viewMode = 'full';
432 break;
433
434 case 'toggle_selection':
435 disableDefault(evt);
436 disablePropagation(evt);
437 var button = target;
438 var link = button.parentNode;
439 link.blur();
440
441 var req = new XMLHttpRequest();
442 var url = link.href;
443 req.open("POST", url, true);
444 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
445 req.send("ajax=1");
446
447 // toggle button
448 var parts = url.split('/');
449 var canonicalImgUrl = parts.slice(0, parts.length-1).join('/');
450
451 if (isAddToSelection.test(url)) {
452 button.src = portal_url() + '/unselect_flag_btn.gif';
453 button.alt = link.title = 'Retirer de la sélection';
454 link.href = canonicalImgUrl + '/remove_to_selection';
455 this.selectedSlide.className = 'selected displayed';
456 this.image.parentNode.className = 'selected';
457 this.selectedSlideInSelection = true;
458 }
459 else {
460 button.src = portal_url() + '/select_flag_btn.gif';
461 button.alt = link.title = 'Ajouter à la sélection';
462 link.href = canonicalImgUrl + '/add_to_selection';
463 this.selectedSlide.className = 'displayed';
464 this.image.parentNode.className = '';
465 this.selectedSlideInSelection = false;
466 }
467 break;
468
469 case 'show_buyable':
470 disableDefault(evt);
471 disablePropagation(evt);
472 var button = target;
473 var link = button.parentNode;
474 link.blur();
475 var slide = this.cartSlide;
476 slide.innerHTML = '';
477 slide.style.visibility = 'visible';
478 var cw = new CartWidget(slide, link.href);
479 cw.onCancel = function() {
480 CartWidget.prototype.onCancel.apply(this);
481 slide.style.visibility = 'hidden';
482 };
483 cw.onAfterConfirm = function() {
484 slide.style.visibility = 'hidden';
485 };
486 break;
487
488
489
490
491 /*
492 case 'edit_metadata' :
493 disableDefault(evt);
494 disablePropagation(evt);
495 target.blur();
496 if (this.viewMode === 'full') {
497 this.mosaique.unload();
498 this.mosaique = null;
499 this.viewMode = 'medium';
500 return;
501 }
502 var fi = new FragmentImporter(absolute_url());
503 fi.useMacro('metadata_edit_form_macros', 'iptc', 'image_metadata');
504 break;
505 */
506 }
507 }
508 };
509
510
511 if(browser.isDOM2Event) {
512 if (browser.isAppleWebKit) {
513 FilmSlider.prototype.mouseWheelHandler = function(evt) {
514 disableDefault(evt);
515 var pos = this.getSliderPosition();
516 pos.x -= evt.wheelDelta / 40;
517 this.setSliderPosition(pos);
518 };
519 }
520 else {
521 FilmSlider.prototype.mouseWheelHandler = function(evt) {
522 disableDefault(evt);
523 var pos = this.getSliderPosition();
524 pos.x += evt.detail * 3;
525 this.setSliderPosition(pos);
526 };
527 }
528 }
529 else if (browser.isIE6up) {
530 FilmSlider.prototype.mouseWheelHandler = function() {
531 var evt = window.event;
532 evt.returnValue = false;
533 var pos = this.getSliderPosition();
534 pos.x -= evt.wheelDelta / 40;
535 this.setSliderPosition(pos);
536 };
537 }
538
539 FilmSlider.prototype.keyDownHandler = function(evt) {
540 var evt = getEventObject(evt);
541 switch (evt.keyCode) {
542 case keyLeft :
543 this.loadSibling(true);
544 break;
545 case keyRight :
546 this.loadSibling(false);
547 break;
548 default:
549 return;
550 }
551 };
552
553
554 FilmSlider.prototype.keyPressHandler = function(evt) {
555 var target = getTargetedObject(evt);
556 if (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA') { return; }
557 var evt = getEventObject(evt);
558 var charPress = String.fromCharCode((evt.keyCode) ? evt.keyCode : evt.which);
559 switch(charPress) {
560 case 'f':
561 case 'F':
562 raiseMouseEvent(this.buttons.full_screen, 'click');
563 break;
564 }
565 };
566
567 FilmSlider.prototype.populateViewer = function(req) {
568 var elements = req.responseXML.documentElement.childNodes;
569 for(var i=0 ; i < elements.length ; i++ ) {
570 element = elements[i];
571 switch (element.nodeName) {
572 case 'fragment' :
573 var dest = document.getElementById(element.getAttribute('id'));
574 if (dest) { dest.innerHTML = element.firstChild.nodeValue; }
575 break;
576 case 'imageattributes' :
577 var link = this.buttons.back_to_portfolio.parentNode;
578 link.href = element.getAttribute('backToContextUrl');
579 link = this.buttons.show_buyable.parentNode;
580 var buyable = element.getAttribute('buyable');
581 if(buyable === 'True') { link.className = null; }
582 else if(buyable === 'False') { link.className = 'hidden'; }
583 this.image.alt = element.getAttribute('alt');
584 this.updateBreadcrumbs(element.getAttribute('lastBcUrl'),
585 element.getAttribute('img_id'));
586 break;
587 }
588 }
589 };
590
591 FilmSlider.prototype.refreshImage = function() {
592 this.image.style.visibility = 'hidden';
593 this.image.src = this.pendingImage.src;
594 this.image.width = this.pendingImage.width;
595 this.image.height = this.pendingImage.height;
596 this.image.style.visibility = 'visible';
597 if (this.selectedSlideInSelection) { this.image.parentNode.className = 'selected'; }
598 else { this.image.parentNode.className = ''; }
599 };
600
601 FilmSlider.prototype.updateBreadcrumbs = function(url, title) {
602 if (this.hasBreadcrumbs) {
603 this.lastBCElement.href = element.getAttribute('lastBcUrl');
604 this.lastBCElement.innerHTML = element.getAttribute('img_id');
605 }
606 };
607
608 FilmSlider.prototype.startSlideShow = function() {
609 this.slideShowSlide = this.pendingSlideShowSlide = this.selectedSlide;
610 return this.slideShowSlide.href;
611 };
612
613 FilmSlider.prototype.slideShowNext = function() {
614 var nextSlide = this.slideShowSlide.parentNode.nextSibling;
615 if (nextSlide && nextSlide.nodeType===3) { nextSlide = nextSlide.nextSibling; }
616
617 if (nextSlide) {
618 nextSlide = nextSlide.getElementsByTagName('a')[0];
619 this.pendingSlideShowSlide = nextSlide;
620 return this.pendingSlideShowSlide.href;
621 }
622 else {
623 var row = this.slideShowSlide.parentNode.parentNode;
624 var first = row.firstChild;
625 if (first.nodeType===3)
626 first = first.nextSibling;
627 this.pendingSlideShowSlide = first.getElementsByTagName('a')[0];
628 return this.pendingSlideShowSlide.href;
629 }
630 };
631
632 FilmSlider.prototype.slideShowPrevious = function() {
633 var previousSlide = this.slideShowSlide.parentNode.previousSibling;
634 if (previousSlide && previousSlide.nodeType===3) { previousSlide = previousSlide.previousSibling; }
635
636 if (previousSlide) {
637 previousSlide = previousSlide.getElementsByTagName('a')[0];
638 this.pendingSlideShowSlide = previousSlide;
639 return this.pendingSlideShowSlide.href;
640 }
641 else {
642 var row = this.slideShowSlide.parentNode.parentNode;
643 var last = row.lastChild;
644 if (last.nodeType===3) { last = last.previousSibling; }
645 this.pendingSlideShowSlide = last.getElementsByTagName('a')[0];
646 return this.pendingSlideShowSlide.href;
647 }
648 };
649
650 FilmSlider.prototype.slideShowImageLoaded = function() {
651 this.slideShowSlide = this.pendingSlideShowSlide;
652 };
653
654 FilmSlider.prototype.stopSlideShow = function() {
655 raiseMouseEvent(this.slideShowSlide, 'click');
656 var index = parseInt(this.selectedSlide.getAttribute('portfolio:position'));
657 this.centerSlide(index);
658 };
659
660
661 /* UTILS */
662 function Point(x, y) {
663 this.x = Math.round(x);
664 this.y = Math.round(y);
665 }
666 Point.prototype.diff = function(point) { return new Point(this.x - point.x, this.y - point.y); };
667 Point.prototype.add = function(point) { return new Point(this.x + point.x, this.y + point.y); };
668 Point.prototype.mul = function(k) { return new Point(this.x * k, this.y *k)};
669 Point.prototype.toString = function() { return "(" + String(this.x) + ", " + String(this.y) + ")"; };
670
671 })();