From f3548c561e8dee04bf8e2d595a9b5ab4206fe003 Mon Sep 17 00:00:00 2001 From: Henning Stahlke Date: Sat, 8 Dec 2018 19:24:19 +0100 Subject: [PATCH] canvas/api.nas: add square, ellipse and circle to path; update svg.nas accordingly --- Nasal/canvas/api.nas | 35 +++++++++++++++++++++++++++++++++++ Nasal/canvas/svg.nas | 4 ++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Nasal/canvas/api.nas b/Nasal/canvas/api.nas index 3b222896a..a00a9e6cf 100644 --- a/Nasal/canvas/api.nas +++ b/Nasal/canvas/api.nas @@ -990,7 +990,42 @@ var Path = { return me.close(); }, + + # Add a (rounded) square to the path + # + # @param x Position of left border + # @param y Position of top border + # @param l length + # @param cfg Optional settings (eg. {"border-top-radius": 5}) + square: func(x, y, l, cfg = nil) { + return me.rect(x, y, l, l, cfg); + }, + # Add an ellipse to the path + # + # @param rx radius x + # @param ry radius y + # @param cx (optional) center x coordinate + # @param cy (optional) center y coordinate + ellipse: func(rx, ry, cx = nil, cy = nil) { + cx = num(cx) or 0; + cy = num(cy) or 0; + me.moveTo(cx - rx, cy) + .arcSmallCW(rx, ry, 0, 2*rx, 0) + .arcSmallCW(rx, ry, 0, -2*rx, 0); + return me; + }, + + + # Add a circle to the path + # + # @param r radius + # @param cx (optional) center x coordinate + # @param cy (optional) center y coordinate + circle: func(r, cx = nil, cy = nil) { + return me.ellipse(r, r, cx, cy); + }, + setColor: func me.setStroke(_getColor(arg)), getColor: func me.getStroke(), diff --git a/Nasal/canvas/svg.nas b/Nasal/canvas/svg.nas index 5d13d7b35..ca6e42696 100644 --- a/Nasal/canvas/svg.nas +++ b/Nasal/canvas/svg.nas @@ -527,14 +527,14 @@ var parsesvg = func(group, path, options = nil) var cx = evalCSSNum(attr['cx']); var cy = evalCSSNum(attr['cy']); var r = evalCSSNum(attr['r']); - stack[-1].moveTo(cx-r,cy).arcSmallCW(r,r,0,2*r,0).arcSmallCW(r,r,0,-2*r,0); + stack[-1].circle(r, cx, cy); } if (name == "ellipse") { var cx = evalCSSNum(attr['cx']); var cy = evalCSSNum(attr['cy']); var rx = evalCSSNum(attr['rx']); var ry = evalCSSNum(attr['ry']); - stack[-1].moveTo(cx-rx,cy).arcSmallCW(rx,ry,0,2*rx,0).arcSmallCW(rx,ry,0,-2*rx,0); + stack[-1].ellipse(rx, ry, cx, cy); } if (name == "path") { parsePath(attr['d']);