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:
August 25th, 2022 at 3:18 pm
zoosk dating login https://onlinedatingservicesecrets.com/
September 3rd, 2022 at 12:16 am
free gay and bi mens chat https://newgaychat.com/
September 3rd, 2022 at 3:37 am
free gay phone chat lines https://gaychatcams.net/
September 3rd, 2022 at 7:27 am
gay free chat https://gaychatspots.com/
September 3rd, 2022 at 4:15 pm
chat avenue gay chat room https://gay-live-chat.net/
September 3rd, 2022 at 9:04 pm
free gay boys chat https://chatcongays.com/
September 3rd, 2022 at 11:53 pm
gay masturbate chat https://gayphillychat.com/
September 4th, 2022 at 4:29 am
gay bear free webcam chat rooms https://gaychatnorules.com/
September 4th, 2022 at 12:08 pm
gay chat video sites https://gaymusclechatrooms.com/
September 4th, 2022 at 4:17 pm
google zoom gay chat rooms https://free-gay-sex-chat.com/
September 4th, 2022 at 6:42 pm
chat cam gay random https://gayinteracialchat.com/
Januar 25th, 2023 at 8:52 pm
3decorum
März 21st, 2023 at 3:01 am
… [Trackback]
[…] Informations on that Topic: ajaxschmiede.de/canvas/regelmaessige-vielecke-mit-canvas/ […]
März 25th, 2023 at 1:05 pm
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://wp.unil.ch/hecimpact/the-facebook-effect-how-facebook-browsing-shapes-the-consumer-decision-making-process/
März 26th, 2023 at 12:47 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://urcinsouthleeds.org.uk/
März 26th, 2023 at 3:01 am
sfwefcs
john
März 26th, 2023 at 5:53 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://sheridancountydemocrats.com/index.php/2021/04/18/alert-from-sheridan-county-democats-citizen-action-teams-on-the-environment-climate-change-and-public-lands/
März 28th, 2023 at 12:11 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://magnumrockmaleenhancement.net
April 10th, 2023 at 2:48 pm
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://www.timesleaderonline.com/sports/local-sports/2015/11/kinnick-s-all-mec-snub-doesn-t-add-up/
April 17th, 2023 at 12:43 pm
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://pomifructifericiumbrud.ro/oyun-pinup-pinball-online-pulsuz-oyna/
April 22nd, 2023 at 8:58 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://www.michelangelo-lavorienoleggi.it/scarpe/nike_air_max_90_premium.html
April 27th, 2023 at 5:41 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://mnsah.org/
Juni 1st, 2023 at 3:28 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://urbanmilwaukee.com/2009/06/20/review-naked-boys-singing-at-turner-hall/
Juni 16th, 2023 at 6:30 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://jackson.extension.colostate.edu/2019/08/19/8-19-19-4-h-memo/
Juli 2nd, 2023 at 12:20 pm
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://hascooil.com/
August 3rd, 2023 at 7:28 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://thebackpackinghiker.com/how-to-cross-the-borders-of-egypt-and-sudan-into-ethiopia/
August 10th, 2023 at 11:59 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://hondanhatrang5s.com/chi-tiet-honda-civic-1-8e-nhap-khau-gia-758-trieu/
August 24th, 2023 at 1:54 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://www.ifsw.org/forums/users/kcwehrm/favorites/
September 14th, 2023 at 5:32 am
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://chicagomuslimsgreenteam.org/category/blog-post/guest
September 18th, 2023 at 12:06 pm
Regelmäßige Vielecke mit Canvas | Ajaxschmiede.de
https://abrosia.com/jacksonville-jaguars-credit-card/