Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Apr 2014
    Messaggi
    323

    da codice a funzione per disegnare triangoli

    Salve, ho questo codice:
    codice:
    <!DOCTYPE HTML> 
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Fill Shapes</title>
    <script>
    function init()
    {
    var canvas = document.getElementById("canvas");
    if(canvas.getContext)
    {
    var ctx = canvas.getContext("2d");
    // Draw triangle
    ctx.fillStyle="#A2322E";
    ctx.beginPath();
    // Draw a triangle location for each corner from x:y 100,110 -> 200,10 -> 300,110 (it will return to first point)
    ctx.moveTo(100,110);
    ctx.lineTo(200,10);
    ctx.lineTo(300,110);
    ctx.closePath();
    ctx.fill();
    }
    }
    onload=init;
    </script>
     
    </head>
     
    <body>
     
    <!-- Unless canvas size is specified default size is 300 w x 150 h -->
    <canvas id="canvas" width="770" height="150" ></canvas>
     
    </body>
     
    </html>
    vorrei fare il modo che posso disegnare triangoli in questo modo:
    drawTriangle(100,110,200,10,300,110);

    sapete come fare?

    avete idea come trasformare questo codice in una funzione per non far ripetere codice etc..

    grazie mille.

  2. #2
    Utente di HTML.it
    Registrato dal
    Apr 2014
    Messaggi
    323
    ho quasi fatto ditemi se vadano bene i nomi che gli ho dato e accetto consigli:
    codice:
    		function drawTriangle(mx,my,x,y,ex,ey,color)
    	{
    		// Draw triangle #A2322E
    		ctx.fillStyle=color;
    		ctx.beginPath();
    		// Draw a triangle location for each corner from x:y 100,110 -> 200,10 -> 300,110 (it will return to first point)
    		ctx.moveTo(mx,my);
    		ctx.lineTo(x,y);
    		ctx.lineTo(ex,ey);
    		ctx.closePath();
    		ctx.fill();
    
    
    	}

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.