Juni 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 15th, 2025 at 11:41 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://pujolbikefit.com/
März 19th, 2025 at 10:25 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://www.climaxtv.net/video/temple-run/
März 19th, 2025 at 12:32 pm
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://stomsarov.ru/без-рубрики/ut-tellus-dolor-dapibus-eget-elementum-vel-cusus/
März 20th, 2025 at 6:46 pm
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
http://lambebaby.com/2018/12/30/christmas-in-albuquerque-2018/
März 31st, 2025 at 8:11 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://ninja-reflection.com/2021/04/19/the-rise-of-phoenixes-novel-translation-127/
April 1st, 2025 at 9:09 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://negj.in/assam-sc-scholarship/
April 2nd, 2025 at 11:37 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://thoitrangphuot.net/gay-leo-nui-gia-re-di-phuot/
April 3rd, 2025 at 8:38 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
http://newgoodsforyou.org/2021/07/15/outdoor-projector-enclosure-2/
April 14th, 2025 at 4:51 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://www.agvbrasilmt.com.br/пиннакл-спорт-обзор/
April 14th, 2025 at 6:01 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://www.siteconstruct.ro/directoare-straine
April 19th, 2025 at 3:54 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://www.bvimedical.com/fi/products/beaver-knives/
April 22nd, 2025 at 4:18 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://pure-homes.com/contact
April 23rd, 2025 at 7:59 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://joliemoore.com/?title=user:gladysfenston88
April 26th, 2025 at 7:08 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://thebabysleepcompany.com.au/
April 29th, 2025 at 11:01 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://centurioncenter.com/catering/7-ways-to-make-your-event-stand-out/
Mai 2nd, 2025 at 10:55 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://iixkkwpvw.blogger.ba/
Mai 7th, 2025 at 3:17 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
http://www.jackleemclean.com/two-paintings-find-new-owners-2/
Mai 8th, 2025 at 4:57 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
http://buyamoxicillin.us.com