Jun 03
Regelmäßige Vielecke mit Canvas
In letzter Zeit habe ich mich verstärkt wieder mal mit Canvas beschäftigt. Dabei ging es mir vor allem um regelmäßige Vielecke (englisch: regular polygons).
Von Haus bietet Canvas in keiner Implementierung eine Funktion zum schnellen und einfachen Erzeugen von regelmäßigen Vielecken. Daher setzte ich mich hin und programmierte angeregt durch canvas.quaese.de zwei entsprechenende canvas-Objekterweiterungen.
Außerdem wollte ich gern gezackte Figuren wie Sterne und Sonnen mit einem Funktionsaufruf erzeugen können. Also nahm ich meinen bisherigen Code her und überarbeitete ihn.
Das Ergebnis all des Codens kann sich meines Erachtens sehen lassen. Vor allem bei Mehrfachaufrufen ergeben sich sehr schicke Muster.
Die Methoden selbst sind nach einem simplen Prinzip aufgebaut. Man gibt die Position des Vieleckmittelpunktes, die Anzahl der Ecken, den Radius (Mittelpunkt->Ecke) und Anfangswinkel (meist 0) an.
Den Quelltext der Objekterweitungen kann ich nachfolgend sehen:
canvas.drawPoly = function(x,y,n,r,phi) { this.translate(x,y); this.rotate(phi); this.beginPath(); this.moveTo(0,r); for( var i=0; i<=n; i++){ this.save(); this.rotate(2*i*Math.PI/n); this.lineTo(0,r); this.restore(); } this.stroke(); this.rotate(-phi); this.translate(-x,-y); } canvas.fillPoly = function(x,y,n,r,phi) { this.translate(x,y); this.rotate(phi); this.beginPath(); this.moveTo(0,r); for( var i=0; i<=n; i++){ this.save(); this.rotate(2*i*Math.PI/n); this.lineTo(0,r); this.restore(); } this.fill(); this.rotate(-phi); this.translate(-x,-y); } canvas.drawZickzack = function(x,y,n,r1,r2,phi) { this.translate(x,y); this.rotate(phi); this.beginPath(); this.moveTo(0,r1); for( var i=0; i<=n; i++){ this.save(); this.rotate(2*i*Math.PI/n); this.lineTo(0,r1); this.restore(); this.save(); this.rotate(2*i*Math.PI/n+(2*Math.PI/n)/2); this.lineTo(0,r2); this.restore(); } this.stroke(); this.rotate(-phi); this.translate(-x,-y); } canvas.fillZickzack = function(x,y,n,r1,r2,phi) { this.translate(x,y); this.rotate(phi); this.beginPath(); this.moveTo(0,r1); for( var i=0; i<=n; i++){ this.save(); this.rotate(2*i*Math.PI/n); this.lineTo(0,r1); this.restore(); this.save(); this.rotate(2*i*Math.PI/n+(2*Math.PI/n)/2); this.lineTo(0,r2); this.restore(); } this.fill(); this.rotate(-phi); this.translate(-x,-y); } // Anwendungsbeispiel: canvas.fillStyle = "#009"; canvas.strokeStyle = "rgb(0,0,0)"; canvas.lineWidth = 2; // Sechseck canvas.drawPoly(60,60,6,40,0);
Im Grundlagenbeitrag zu Canvas kann man sich genauere Informationen zum Einbau holen. Auch ein Blick in den Quellcode meiner angefertigen Demo hilft sich dem ein oder anderen und zeigt die Möglichkeiten der Methoden auf.
Ähnliche Themen im Blog:
März 2nd, 2022 at 3:33 pm
clindamycin cats
clindamycin cats
März 2nd, 2022 at 9:20 pm
zithromax antibiotic
zithromax antibiotic
März 3rd, 2022 at 5:34 am
ciprofloxacin gastritis
ciprofloxacin gastritis
März 5th, 2022 at 9:28 pm
celecoxib with excedrin
celecoxib with excedrin
Juni 19th, 2022 at 5:09 am
1slavish
Juli 3rd, 2022 at 8:38 pm
sildenafil
sildenafil
Juli 5th, 2022 at 12:53 pm
viagra
viagra
Juli 6th, 2022 at 2:35 pm
sildenafil citrate
sildenafil citrate
Juli 11th, 2022 at 4:17 am
revatio
revatio
Juli 25th, 2022 at 4:41 pm
admiral online casino https://download-casino-slots.com/
Juli 25th, 2022 at 6:49 pm
fastest payout online casino usa https://firstonlinecasino.org/
Juli 25th, 2022 at 10:33 pm
online casino no deposit bonus 2021 https://onlinecasinofortunes.com/
Juli 26th, 2022 at 1:34 am
online casino virginia https://newlasvegascasinos.com/
Juli 26th, 2022 at 4:00 am
nj online casino no deposit bonus https://trust-online-casino.com/
Juli 26th, 2022 at 4:47 am
best online casino sites https://onlinecasinosdirectory.org/
Juli 26th, 2022 at 6:57 am
diamond reels online casino https://9lineslotscasino.com/
Juli 26th, 2022 at 9:29 am
caesars online casino nj https://free-online-casinos.net/
Juli 26th, 2022 at 12:40 pm
lucky 7 casino online https://internet-casinos-online.net/
Juli 26th, 2022 at 3:29 pm
michigan online casino free play https://cybertimeonlinecasino.com/
Juli 26th, 2022 at 5:22 pm
wind creek casino online games homepage https://1freeslotscasino.com/
Juli 26th, 2022 at 9:32 pm
atlantic city online casino https://vrgamescasino.com/
Juli 27th, 2022 at 12:42 am
online casino ny real money no deposit https://casino-online-roulette.com/
Juli 27th, 2022 at 2:42 am
online casino real money free play https://casino-online-jackpot.com/
Juli 27th, 2022 at 4:10 am
virgin casino online nj https://onlineplayerscasino.com/
Juli 27th, 2022 at 8:39 am
el royale online casino review https://ownonlinecasino.com/
Juli 27th, 2022 at 11:02 am
top online casino https://all-online-casino-games.com/
Juli 27th, 2022 at 1:45 pm
online casino big winners https://casino8online.com/
August 7th, 2022 at 1:19 pm
free vpn https://freevpnconnection.com/
August 7th, 2022 at 3:12 pm
avast secure line vpn https://shiva-vpn.com/
August 7th, 2022 at 5:04 pm
vpn for windows free download https://freehostingvpn.com/
August 7th, 2022 at 8:44 pm
tunnelbear free vpn https://ippowervpn.net/
August 7th, 2022 at 11:19 pm
fastest vpn service https://imfreevpn.net/
August 8th, 2022 at 12:07 am
best chrome vpn free https://superfreevpn.net/
August 8th, 2022 at 2:30 am
best free vpn cnet https://free-vpn-proxy.com/
August 8th, 2022 at 5:13 am
trust zone vpn https://rsvpnorthvalley.com/