Parcourir la source

Taking all vertices into account when determining if the image is inside the canvas

Patrick Augusto il y a 4 ans
Parent
commit
ba8c3c0c13
1 fichiers modifiés avec 8 ajouts et 1 suppressions
  1. 8 1
      Pixel.js

+ 8 - 1
Pixel.js

@@ -12,6 +12,13 @@ const isInside = function(vertex, square) {
         && (0 <= dotProduct(BC, BM) && dotProduct(BC, BM) <= dotProduct(BC, BC));
 };
 
+const isInsideSquare = function(vertex, square, isize) {
+    return isInside(vertex, square)
+        || isInside({x: vertex.x + isize.width, y: vertex.y}, square)
+        || isInside({x: vertex.x,               y: vertex.y + isize.height}, square)
+        || isInside({x: vertex.x + isize.width, y: vertex.y + isize.height}, square);
+};
+
 const toBinary = function(decimal, padding) {
     return decimal.toString(2).padStart(padding, 0);
 };
@@ -62,7 +69,7 @@ const draw = function(ctx, csize, isize, url, level, images, coords) {
 const draw_ = function(ctx, isize, url, level, images, id, coords) {
     const decimal = parseInt(id, 2);
 
-    if (!isInside(coords, canvas.vertices)) {
+    if (!isInsideSquare(coords, canvas.vertices, isize)) {
         return images;
     } else {
         const index = images.findIndex((i) => (url + id) == i.image.src);