From 9872bf09cdf89aa313c7cb8ae73bc21af3ccc786 Mon Sep 17 00:00:00 2001 From: Till Arnold Date: Fri, 16 Feb 2018 11:39:37 +0100 Subject: [PATCH] Add `strokeCircle`, `fillCircle` and `rectCenteredAt` --- README.md | 9 +++++++++ index.js | 13 +++++++++++++ test/beefy.js | 6 ++---- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c0c5fe5..e297a40 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,12 @@ Clears the canvas. If you have applied any transforms to the context this may no ### l.circle(x, y, r) Draws a path in form of a circle at `x`/`y` with a radius of `r`. +### l.strokeCircle(x, y, r) +Strokes a circle at `x`/`y` with a radius of `r`. + +### l.fillCircle(x, y, r) +Fills a circle at `x`/`y` with a radius of `r`. + ### l.rotateContextAt(x, y, r) Rotates the context at `x`/`y` by `r` radians. @@ -119,6 +125,9 @@ Resets all the transforms. ### l.clearWithTransforms() Clears the canvas event if there have been transforms applied. The tansforms are preserved. +### l.rectCenteredAt(x, y, w, h) +Draws a path of a rectangle centered at `x`/`y` with a widht of `w` and a hight of `h`. + ### l.fillRectCenteredAt(x, y, w, h) Fills a rectangle centered at `x`/`y` with a widht of `w` and a hight of `h`. diff --git a/index.js b/index.js index 8d60b2a..01016b1 100644 --- a/index.js +++ b/index.js @@ -111,6 +111,14 @@ Leinwand.prototype.circle = function cirlce(x, y, r) { return this.arc(x, y, r, 0, 2 * Math.PI, false); }; +Leinwand.prototype.strokeCircle = function strokeCircle(x, y, r) { + return this.beginPath().circle(x, y, r).closePath().stroke(); +}; + +Leinwand.prototype.fillCircle = function fillCircle(x, y, r) { + return this.beginPath().circle(x, y, r).closePath().fill(); +}; + Leinwand.prototype.rotateContextAt = function rotateContextAt(x, y, r) { return this .translate(x, y) @@ -135,6 +143,11 @@ Leinwand.prototype.clearWithTransforms = function clearWithTransforms() { .restore(); }; +Leinwand.prototype.rectCenteredAt = function rectCenteredAt(x, y, w, h) { + return this + .rect(x - w / 2, y - h / 2, w, h); +}; + Leinwand.prototype.fillRectCenteredAt = function fillRectCenteredAt(x, y, w, h) { return this .fillRect(x - w / 2, y - h / 2, w, h); diff --git a/test/beefy.js b/test/beefy.js index 8a56d62..860261f 100644 --- a/test/beefy.js +++ b/test/beefy.js @@ -26,11 +26,9 @@ img.onload = () => { .closePath() .fill() .stroke() - .beginPath() .fillStyle('blue') - .circle(50, 50, 40) - .fill() - .stroke() + .fillCircle(50, 50, 40) + .strokeCircle(50, 50, 40) .fillRect(200, 200, 100, 100) .rotateContextAt(250, 250, Math.PI / 4) .fillRect(200, 200, 100, 100)