Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Generuj jednoduchý webgpu bez frameworků obsahující koláčový graf zobrazující tři…

  1. User

    Generuj jednoduchý webgpu bez frameworků obsahující koláčový graf zobrazující tři položky. 1. 20% zelená barva 2. 50% modrá barva a 3. 30% červená barva. Dále možnost upravovat procenta přes inputy

  2. Assistant

    Zde je příklad jednoduchého webgpu bez použití frameworku:

    <!DOCTYPE html>
    <html>
    <head>
        <title>Koláčový graf</title>
        <style>
            canvas {
                width: 500px;
                height: 500px;
            }
            input {
                margin-top: 10px;
            }
        </style>
    </head>
    <body>
        <canvas id="graf"></canvas>
        <div>
            <label for="zeleny-input">Zelená:</label>
            <input type="number" id="zeleny-input" min="0" max="100" value="20">
        </div>
        <div>
            <label for="modry-input">Modrá:</label>
            <input type="number" id="modry-input" min="0" max="100" value="50">
        </div>
        <div>
            <label for="cerveny-input">Červená:</label>
            <input type="number" id="cerveny-input" min="0" max="100" value="30">
        </div>
    
        <script>
            const canvas = document.getElementById('graf');
            const ctx = canvas.getContext('2d');
            const zelenyInput = document.getElementById('zeleny-input');
            const modryInput = document.getElementById('modry-input');
            const cervenyInput = document.getElementById('cerveny-input');
            
            function vykresliGraf() {
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                
                let zeleny = zelenyInput.value;
                let modry = modryInput.value;
                let cerveny = cervenyInput.value;
                
                // převod procent na úhly
                let zelenyUhly = (zeleny / 100) * Math.PI * 2;
                let modryUhly = (modry / 100) * Math.PI * 2;
                let cervenyUhly = (cerveny / 100) * Math.PI * 2;
                
                // nastavení pozice a velikosti grafu
                let x = canvas.width / 2;
                let y = canvas.height / 2;
                let radius = Math.min(canvas.width, canvas.height) / 2 - 10;
                
                // vykreslení tří sektorů
                ctx.beginPath();
                ctx.moveTo(x, y);
                ctx.arc(x, y, radius, 0, zelenyUhly);
                ctx.fillStyle = 'green';
                ctx.fill();
                
                ctx.beginPath();
                ctx.moveTo(x, y);
                ctx.arc(x, y, radius, zelenyUhly, zelenyUhly + modryUhly);
                ctx.fillStyle = 'blue';
                ctx.fill();
                
                ctx.beginPath();
                ctx.moveTo(x, y);
                ctx.arc(x, y, radius, zelenyUhly + modryUhly, zelenyUhly + modryUhly + cervenyUhly);
                ctx.fillStyle = 'red';
                ctx.fill();
            }
            
            // při změně hodnoty vstupů vykreslit graf
            zelenyInput.addEventListener('input', vykresliGraf);
            modryInput.addEventListener('input', vykresliGraf);
            cervenyInput.addEventListener('input', vykresliGraf);
            
            // vykreslit graf při načtení stránky
            vykresliGraf();
        </script>
    </body>
    </html>
    

    Tento kód vytvoří jednoduchou stránku s koláčovým grafem, který zobrazuje tři položky s danými procenty barev (20% zelené, 50% modré a 30% červené). Dále obsahuje tři vstupní pole, která umožňují upravit tyto procenta. Při změně hodnoty vstupu se graf automaticky aktualizuje.

  3. User

    Vykresli kruh pomocí webgpu bez frameworků

  4. Assistant

    Vykreslení kruhu pomocí WebGPU vyžaduje důkladnější implementaci a manipulaci s grafickými zdroji a příkazy. Zde je jednoduchý příklad kódu, který vytváří WebGPU kontext a vykresluje kruh:

    <!DOCTYPE html>
    <html>
    <head>
        <title>Vykreslení kruhu pomocí WebGPU</title>
        <style>
            canvas {
                width: 500px;
                height: 500px;
            }
        </style>
    </head>
    <body>
        <canvas id="canvas"></canvas>
    
        <script>
            // vytvoření canvas elementu a získání WebGPU kontextu
            const canvas = document.getElementById('canvas');
            const gpu = canvas.getContext('webgpu');
    
            // popis kruhu
            const circleDescriptor = {
                radius: 100,
                color: [1, 0, 0, 1], // červená barva
                position: [canvas.width / 2, canvas.height / 2], // střed canvasu
            };
    
            // inicializace WebGPU
            async function init() {
                // vytvoření adaptéru a zařízení
                const adapter = await navigator.gpu.requestAdapter();
                const device = await adapter.requestDevice();
    
                // vytvoření výstupního bufferu a jeho popisu
                const textureFormat = 'bgra8unorm';
                const [width, height] = [canvas.width, canvas.height];
                const texture = device.createTexture({
                    size: { width, height },
                    format: textureFormat,
                    usage: GPUTextureUsage.OUTPUT_ATTACHMENT,
                });
    
                const context = canvas.getContext('gpupresent');
                context.configure({
                    device,
                    format: textureFormat,
                });
    
                // definování pipeline
                const pipeline = device.createRenderPipeline({
                    vertex: {
                        module: device.createShaderModule({ code: vertexShaderCode() }),
                        entryPoint: 'main',
                    },
                    fragment: {
                        module: device.createShaderModule({ code: fragmentShaderCode() }),
                        entryPoint: 'main',
                        targets: [
                            {
                                format: textureFormat,
                            },
                        ],
                    },
                    primitive: {
                        topology: 'point-list',
                    },
                });
    
                // vytvoření command encoderu a příprava na vykreslení
                const commandEncoder = device.createCommandEncoder();
                const passEncoder = commandEncoder.beginRenderPass({
                    colorAttachments: [
                        {
                            view: texture.createView(),
                            loadValue: [0, 0, 0, 1], // černé pozadí
                        },
                    ],
                });
    
                // práce s bufferem - vykreslení kruhu
                const positionBuffer = device.createBuffer({
                    size: circleDescriptor.position.length * 4,
                    usage: GPUBufferUsage.VERTEX,
                    mappedAtCreation: true,
                });
    
                new Float32Array(positionBuffer.getMappedRange()).set(circleDescriptor.position);
                positionBuffer.unmap();
    
                const colorBuffer = device.createBuffer({
                    size: circleDescriptor.color.length * 4,
                    usage: GPUBufferUsage.VERTEX,
                    mappedAtCreation: true,
                });
    
                new Float32Array(colorBuffer.getMappedRange()).set(circleDescriptor.color);
                colorBuffer.unmap();
    
                passEncoder.setPipeline(pipeline);
                passEncoder.setVertexBuffer(0, positionBuffer);
                passEncoder.setVertexBuffer(1, colorBuffer);
                passEncoder.draw(1);
    
                // ukončení vykreslování a odeslání příkazů
                passEncoder.endPass();
                device.defaultQueue.submit([commandEncoder.finish()]);
            }
    
            init();
    
            // Vertex shader kód
            function vertexShaderCode() {
                return `
                    struct VertexInput {
                        [[location(0)]] position: vec2<f32>;
                        [[location(1)]] color: vec4<f32>;
                    };
    
                    struct VertexOutput {
                        [[builtin(position)]] position: vec4<f32>;
                        [[location(0)]] color: vec4<f32>;
                    };
    
                    [[stage(vertex)]]
                    fn main(input: VertexInput) -> VertexOutput {
                        var output: VertexOutput;
                        output.position = vec4<f32>(input.position, 0.0, 1.0);
                        output.color = input.color;
                        return output;
                    }
                `;
            }
    
            // Fragment shader kód
            function fragmentShaderCode() {
                return `
                    struct FragmentInput {
                        [[location(0)]] color: vec4<f32>;
                    };
    
                    [[stage(fragment)]]
                    fn main(input: FragmentInput) -> [[location(0)]] vec4<f32> {
                        return input.color;
                    }
                `;
            }
        </script>
    </body>
    </html>
    

    Tento kód vytvoří canvas element a získá WebGPU kontext. Poté inicializuje WebGPU a vytvoří shader moduly pro vertex a fragment shader. Pro kruh jsou vytvořeny bufferové objekty pro pozici a barvu, a ty jsou předány do vertex shaderu. Nakonec je vykreslení kruhu realizováno vykreslovacími příkazy.

  5. User

    totéž ale pouze přes css

  6. Assistant

    Bohužel není možné vykreslit kruh pomocí WebGPU pouze pomocí CSS. WebGPU je grafické API, které slouží jako alternativa k WebGL, a vyžaduje přímou manipulaci s grafickými zdroji a příkazy pomocí JavaScriptu. CSS slouží především k popisu stylování webového rozhraní, ale nemá přímou podporu pro vytváření a vykreslování grafičkých objektů, jako je kruh.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.