HTML Canvas İle Küp Çizmek

Whistle

Admin
Kurucu
Süper Üye
Geliştirici
Yardımsever Üye
Mesaj
12.281
Çözümler
584
Beğeni
9.548
Puan
8.781
Ticaret Puanı
3
Merhaba arkadaşlar,
Birkaç gün önce denemek için hazırladığım kodları sizle paylaşmak istiyorum.

tek küp.jpg


JavaScript:
Genişlet Daralt Kopyala
<canvas id="game" width="1280" height="724"></canvas>

<script>
class Kup3D {
    constructor() {
        this.canvas = document.getElementById('game');
        this.context = this.canvas.getContext('2d');
        this.kupAdetX = 3;
        this.kupAdetY = 3;
        this.kupAdetZ = 3;
   
        this.kareBoyut = 150;
    }
 
    init () {
        this.timer = setInterval(this.loop.bind(this), 1000 / 15);
    }
 
    reset() {
        clearInterval(this.timer);
        this.init();
    }
 
    loop() {
        this.draw();
    }
 
    draw() {
        this.context.fillStyle = 'green';
        this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
   
        for(var x = 0; x < this.kupAdetX; x++)
        {
            for(var y = 0; y < this.kupAdetY; y++)
            {
                for(var z = 0; z < this.kupAdetZ; z++)
                {
                    //küp on yuzu ciz.
                    this.context.beginPath();
                    this.context.strokeStyle  = 'rgba(255, 225, 225, 1)';
                    this.context.rect(100+(x*this.kareBoyut)+(z*50), 200+(y*this.kareBoyut)-(z*30), this.kareBoyut, this.kareBoyut);
                    this.context.stroke();
               
                    //küp arka tarafini ciz.
                    this.context.beginPath();
                    this.context.strokeStyle  = 'rgba(255, 225, 225, 1)';
                    this.context.rect(150+(x*this.kareBoyut)+(z*50), 170+(y*this.kareBoyut)-(z*30), this.kareBoyut, this.kareBoyut);
                    this.context.stroke();
               
                    //sol üst yan çizgi
                    this.context.beginPath();
                    this.context.strokeStyle = 'rgba(255, 225, 225, 1)';
                    this.context.moveTo(150+(x*this.kareBoyut)+(z*50), 170+(y*this.kareBoyut)-(z*30));
                    this.context.lineTo(100+(x*this.kareBoyut)+(z*50), 200+(y*this.kareBoyut)-(z*30));
                    this.context.stroke();
               
                    //sağ üst yan çizgi
                    this.context.beginPath();
                    this.context.strokeStyle = 'rgba(255, 225, 225, 1)';
                    this.context.moveTo(300+(x*this.kareBoyut)+(z*50), 170+(y*this.kareBoyut)-(z*30));
                    this.context.lineTo(250+(x*this.kareBoyut)+(z*50), 200+(y*this.kareBoyut)-(z*30));
                    this.context.stroke();
               
                    //sol alt çizgi.
                    this.context.beginPath();
                    this.context.strokeStyle = 'rgba(255, 225, 225, 1)';
                    this.context.moveTo(150+(x*this.kareBoyut)+(z*50), 320+(y*this.kareBoyut)-(z*30));
                    this.context.lineTo(100+(x*this.kareBoyut)+(z*50), 350+(y*this.kareBoyut)-(z*30));
                    this.context.stroke();
               
                    //sağ alt çizgi.
                    this.context.beginPath();
                    this.context.strokeStyle = 'rgba(255, 225, 225, 1)';
                    this.context.moveTo(300+(x*this.kareBoyut)+(z*50), 320+(y*this.kareBoyut)-(z*30));
                    this.context.lineTo(250+(x*this.kareBoyut)+(z*50), 350+(y*this.kareBoyut)-(z*30));
                    this.context.stroke();
                }
            }
        }
    }
}

const game = new Kup3D();
window.onload = () => game.init();

</script>

3x3x3 küpler.jpg
 
gerçek hayatta ne işimize yarayacak hocam bu .d
 
gerçek hayatta ne işimize yarayacak hocam bu .d


Websitenin arkaplanı için buna benzer bir background oluşturabilirsin.Fotoğraf olmadığı için daha az kaynak tüketir ve sistemin daha hızlı çalışır.
SbV7qwk7jkrfhey8wWZ4Mb.webp
 
Uyarı: Bu konu açıldığından bu yana baya zaman geçmiş.
Muhtemelen daha fazla tartışma gerekli değildir ki bu durumda yeni bir konu başlatmayı öneririz. Eğer yine de cevabınızın gerekli olduğunu düşünüyorsanız buna rağmen cevap verebilirsiniz.
Geri
Üst