Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.3 kB
2
Indexable
Never
        function drawCube(ctx, x, y, size) {
            // Draw a cube
            ctx.fillStyle = 'red';
            ctx.fillRect(x, y, size, size);
        }

        function drawSphere(ctx, x, y, radius) {
            // Draw a sphere
            ctx.fillStyle = 'purple';
            ctx.beginPath();
            ctx.arc(x, y, radius, 0, 2 * Math.PI);
            ctx.closePath();
            ctx.fill();
        }

        function drawCylinder(ctx, x, y, radius, height) {
            // Draw a cylinder
            ctx.fillStyle = 'orange';
            ctx.beginPath();
            ctx.arc(x, y, radius, 0, 2 * Math.PI);
            ctx.closePath();
            ctx.fill();

            ctx.fillStyle = 'yellow';
            ctx.fillRect(x - radius, y, radius * 2, height);
        }

        function performDrawing() {
            const canvas = document.createElement('canvas');
            canvas.width = 400;
            canvas.height = 400;

            document.body.appendChild(canvas);

            const ctx = canvas.getContext('2d');
            ctx.imageSmoothingEnabled = false; // Disable image smoothing for better performance

            const attempts = 10;
            let totalDrawingTime = 0;

            for (let i = 0; i < attempts; i++) {
                const startTime = performance.now();

                // Draw a cube
                drawCube(ctx, 100, 100, 80);

                // Draw a sphere
                drawSphere(ctx, 200, 250, 60);

                // Draw a cylinder
                drawCylinder(ctx, 320, 180, 40, 120);

                const endTime = performance.now();
                const elapsedTime = endTime - startTime;
                totalDrawingTime += elapsedTime;
            }

            const averageDrawingTime = totalDrawingTime / attempts;
            console.log('Average Drawing Time:', averageDrawingTime.toFixed(2), 'ms');
        }

        performDrawing();performDrawing();performDrawing();performDrawing();performDrawing();performDrawing();performDrawing();performDrawing();performDrawing();performDrawing();performDrawing();performDrawing();performDrawing();performDrawing();performDrawing();performDrawing();performDrawing();performDrawing();performDrawing();performDrawing();