about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-07-05 11:32:45 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-07-05 11:38:08 -0700
commit26a98d027bfb4c65fbc0095b96b99e0732fcc671 (patch)
treec10300767d690afa9d109790d3989400c4afab1a
parent4575648c12beba741b858b2fcdf1a195717e9d2e (diff)
downloadview.love-26a98d027bfb4c65fbc0095b96b99e0732fcc671.tar.gz
make freehand drawings smoother
Now I might actually use them more, and maybe I can start considering
taking out some shapes. Do I really need circles if I don't provide
ellipses?

Thanks Ivan Reese for the feedback. "What drawings does your tool
encourage?"

Minor note: taking out the deepcopy creates a cute little string like
effect, where the curve grows tighter the slower you draw it.
-rw-r--r--drawing.lua18
1 files changed, 17 insertions, 1 deletions
diff --git a/drawing.lua b/drawing.lua
index cff5620..3ed1b3f 100644
--- a/drawing.lua
+++ b/drawing.lua
@@ -116,7 +116,9 @@ function Drawing.draw_pending_shape(left,top, drawing)
   if shape.mode == nil then
     -- nothing pending
   elseif shape.mode == 'freehand' then
-    Drawing.draw_shape(left,top, drawing, shape)
+    local shape_copy = deepcopy(shape)
+    Drawing.smoothen(shape_copy)
+    Drawing.draw_shape(left,top, drawing, shape_copy)
   elseif shape.mode == 'line' then
     local mx,my = Drawing.coord(App.mouse_x()-left), Drawing.coord(App.mouse_y()-top)
     if mx < 0 or mx >= 256 or my < 0 or my >= drawing.h then
@@ -291,6 +293,7 @@ function Drawing.mouse_released(x,y, button)
         -- nothing pending
       elseif drawing.pending.mode == 'freehand' then
         -- the last point added during update is good enough
+        Drawing.smoothen(drawing.pending)
         table.insert(drawing.shapes, drawing.pending)
       elseif drawing.pending.mode == 'line' then
         local mx,my = Drawing.coord(x-Margin_left), Drawing.coord(y-drawing.y)
@@ -673,6 +676,19 @@ function Drawing.contains_point(shape, p)
   end
 end
 
+function Drawing.smoothen(shape)
+  assert(shape.mode == 'freehand')
+  for _=1,7 do
+    for i=2,#shape.points-1 do
+      local a = shape.points[i-1]
+      local b = shape.points[i]
+      local c = shape.points[i+1]
+      b.x = (a.x + b.x + c.x)/3
+      b.y = (a.y + b.y + c.y)/3
+    end
+  end
+end
+
 function Drawing.insert_point(points, x,y)
   for i,point in ipairs(points) do
     if Drawing.near(point, x,y) then