From 2a5ae4d9850ebfe20e43a3faa860ef551b3b2805 Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Thu, 21 May 2015 14:42:18 +0200 Subject: [PATCH] Translate box so its well placed --- static/js/prototype/Previewer.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/static/js/prototype/Previewer.js b/static/js/prototype/Previewer.js index 60b6136..23e97df 100644 --- a/static/js/prototype/Previewer.js +++ b/static/js/prototype/Previewer.js @@ -1,3 +1,7 @@ +Math.clamp = Math.clamp || function(number, min, max) { + return Math.max(Math.min(number, max), min); +} + var Previewer = function(renderer) { this.domElement = document.createElement('canvas'); this.ctx = this.domElement.getContext('2d'); @@ -14,11 +18,15 @@ Previewer.prototype.render = function(prev, container_width, container_height) { if (!this.fixed) { left = Math.floor(prev.x - width/2); bottom = Math.floor(this.renderer.domElement.height - prev.y + height/5); + + // Translate box if too high if (bottom + height > this.renderer.domElement.height) { bottom -= 7 * height / 5; - console.log(bottom); } - console.log(bottom, this.renderer.domElement.height); + + // Translate box if too on the side + left = Math.clamp(left, width / 5, this.renderer.domElement.width - 6 * width / 5); + } else { left = 0; bottom = 0;