Skip to content
TheChatArchiveThe Chat Archive
grok-3-mini-high· xAIHugging Face LMArena Human Preference 140K

Create a HTML JavaScript editor which uses konva.js (https://konvajs.org/),…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with o3-2025-04-16’s answer.
  1. User

    Create a HTML JavaScript editor which uses konva.js (https://konvajs.org/), pickr(https://github.com/Simonwep/pickr) and font-awesome (https://github.com/FortAwesome/Font-Awesome). Also you need to use FontFaceObserver if you need to use none standard fonts. (https://fontfaceobserver.com/)

  2. Assistant

    Below is a complete HTML file that implements a simple JavaScript-based editor using the specified libraries: Konva.js for canvas manipulation, Pickr for color picking, and Font Awesome for icons. I've also incorporated FontFaceObserver to handle non-standard font loading when adding text elements. This editor allows users to:

    • Add rectangles and text elements to a canvas.
    • Select shapes or text to manipulate them (e.g., move, resize).
    • Use a color picker to change the fill color of selected elements.
    • Choose from a list of fonts (including non-standard ones like "Roboto" from Google Fonts), with FontFaceObserver ensuring the font is loaded before rendering.

    The editor is self-contained in a single HTML file, with all libraries loaded via CDNs. I've included basic CSS for layout and styling. Here's a quick overview of the features:

    • Toolbar: Contains buttons with Font Awesome icons to add rectangles or text, a font selection dropdown, and a color picker.
    • Canvas: Uses Konva.js to render interactive elements. Shapes and text are draggable and selectable.
    • Color Picker: Integrated with Pickr; changes the fill color of selected elements.
    • Font Handling: When adding text, FontFaceObserver loads the selected font (e.g., "Roboto") before creating the text node. If the font fails to load, it falls back to "Arial".

    Code

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Konva.js Editor with Pickr and Font Awesome</title>
    
        <!-- Font Awesome CSS for icons -->
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    
        <!-- Pickr CSS for color picker -->
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@simonwep/pickr/dist/pickr.min.css">
    
        <!-- Google Fonts for non-standard font example (Roboto) -->
        <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
    
        <!-- Custom CSS for layout -->
        <style>
            body {
                font-family: Arial, sans-serif;
                margin: 0;
                padding: 10px;
            }
            #toolbar {
                display: flex;
                align-items: center;
                gap: 10px;
                margin-bottom: 10px;
            }
            #container {
                border: 1px solid #ccc;
                width: 800px;
                height: 600px;
            }
            button {
                padding: 10px;
                font-size: 14px;
                cursor: pointer;
            }
            select {
                padding: 5px;
            }
        </style>
    
        <!-- Konva.js script -->
        <script src="https://unpkg.com/konva@8.3.5/konva.min.js"></script>
    
        <!-- Pickr script for color picker -->
        <script src="https://cdn.jsdelivr.net/npm/@simonwep/pickr/dist/pickr.min.js"></script>
    
        <!-- FontFaceObserver script for font loading -->
        <script src="https://cdn.jsdelivr.net/npm/fontfaceobserver/fontfaceobserver.standalone.js"></script>
    </head>
    <body>
    
        <!-- Toolbar with buttons, color picker, and font selector -->
        <div id="toolbar">
            <button id="addRect"><i class="fas fa-square"></i> Add Rectangle</button>
            <button id="addText"><i class="fas fa-font"></i> Add Text</button>
            <div class="color-picker"></div> <!-- Container for Pickr color picker -->
            <select id="fontSelect">
                <option value="Arial">Arial (Standard)</option>
                <option value="Times New Roman">Times New Roman (Standard)</option>
                <option value="Roboto">Roboto (Non-Standard, Loaded via Google Fonts)</option>
            </select>
        </div>
    
        <!-- Container for Konva.js stage -->
        <div id="container"></div>
    
        <!-- JavaScript code for the editor -->
        <script>
            document.addEventListener('DOMContentLoaded', function() {
                // Set up Konva stage and layer
                var stage = new Konva.Stage({
                    container: 'container',
                    width: 800,
                    height: 600
                });
                var layer = new Konva.Layer();
                stage.add(layer);
    
                // Add transformer for shape selection and manipulation
                var tr = new Konva.Transformer();
                layer.add(tr);
    
                // Handle click events for shape selection
                layer.on('click', function(evt) {
                    var shape = evt.target;
                    if (shape === layer) {
                        // Deselect if clicking on the layer background
                        tr.nodes([]);
                    } else if (shape.getClassName() !== 'Transformer') {
                        // Select the shape and attach transformer
                        tr.nodes([shape]);
                    }
                    layer.draw();
                });
    
                // Add rectangle button event listener
                document.getElementById('addRect').addEventListener('click', function() {
                    var rect = new Konva.Rect({
                        x: 50,
                        y: 50,
                        width: 100,
                        height: 50,
                        fill: 'red',
                        stroke: 'black',
                        strokeWidth: 2,
                        draggable: true // Allow dragging
                    });
                    layer.add(rect);
                    layer.draw();
                });
    
                // Add text button event listener with font loading
                document.getElementById('addText').addEventListener('click', function() {
                    var fontFamily = document.getElementById('fontSelect').value;
                    var fontObserver = new FontFaceObserver(fontFamily);
    
                    // Load the font using FontFaceObserver
                    fontObserver.load().then(function() {
                        console.log('Font loaded: ' + fontFamily);
                        var text = new Konva.Text({
                            x: 100,
                            y: 100,
                            text: 'Hello World', // Default text; could be made editable
                            fontSize: 20,
                            fontFamily: fontFamily,
                            fill: 'black',
                            draggable: true // Allow dragging
                        });
                        layer.add(text);
                        layer.draw();
                    }, function() {
                        console.error('Font load failed for ' + fontFamily + '. Falling back to Arial.');
                        // Fallback to a standard font if loading fails
                        var text = new Konva.Text({
                            x: 100,
                            y: 100,
                            text: 'Hello World',
                            fontSize: 20,
                            fontFamily: 'Arial',
                            fill: 'black',
                            draggable: true
                        });
                        layer.add(text);
                        layer.draw();
                    });
                });
    
                // Set up Pickr color picker
                var pickr = Pickr.create({
                    el: '.color-picker', // Attach to the div with class "color-picker"
                    theme: 'classic', // Use the classic theme
                    default: '#ff0000', // Default color is red
                    swatches: [ // Optional: Add some color swatches for quick selection
                        'rgba(244, 67, 54, 1)', // Red
                        'rgba(33, 150, 243, 1)', // Blue
                        'rgba(76, 175, 80, 1)' // Green
                    ],
                    components: {
                        preview: true,
                        opacity: true,
                        hue: true,
                        interaction: {
                            hex: true,
                            rgba: true,
                            hsla: true,
                            hsva: true,
                            cmyk: true,
                            input: true,
                            clear: true,
                            save: true
                        }
                    }
                });
    
                // Handle color change event from Pickr
                pickr.on('change', (color) => {
                    var selectedNodes = tr.nodes(); // Get the currently selected nodes
                    if (selectedNodes.length > 0) {
                        var hexColor = color.toHEXA().toString(); // Convert Pickr color to HEXA string
                        selectedNodes.forEach(function(node) {
                            if (node.fill) { // Check if the node has a fill property (e.g., Rect or Text)
                                node.fill(hexColor); // Apply the new fill color
                            }
                        });
                        layer.batchDraw(); // Efficiently redraw the layer
                    }
                });
            });
        </script>
    </body>
    </html>
    

    How to Use

    1. Open the HTML file in a browser: Save the code to a file (e.g., editor.html) and open it in a modern web browser (Chrome, Firefox, etc.).
    2. Toolbar Interactions:
      • Click the "Add Rectangle" button (square icon) to add a red rectangle to the canvas.
      • Click the "Add Text" button (font icon) to add text. Use the font dropdown to select a font (e.g., "Roboto" for a non-standard font). FontFaceObserver will load the font before rendering.
      • Use the color picker to select a color and apply it to any selected shape or text. Click on a shape or text on the canvas to select it (handles will appear for resizing and moving).
    3. Canvas Interactions:
      • Shapes and text are draggable. Click and drag to move them.
      • Selection: Click a shape to select it. The transformer (handles) will appear, allowing resizing. Deselect by clicking the canvas background.
    4. Font Loading:
      • For non-standard fonts like "Roboto", FontFaceObserver ensures the font is loaded before adding the text node. If the font fails to load (e.g., due to network issues), it falls back to "Arial".
      • The "Roboto" font is loaded via Google Fonts in the HTML head. You can add more fonts by including additional Google Font links and options in the dropdown.

    Notes

    • Libraries Used:
      • Konva.js: Handles the canvas and interactive elements.
      • Pickr: Provides the color picker interface.
      • Font Awesome: Used for icons in the toolbar buttons.
      • FontFaceObserver: Used to load fonts asynchronously, ensuring compatibility with non-standard fonts.
    • Limitations and Enhancements:
      • This is a basic editor. You could extend it by adding more shapes, text editing (e.g., double-click to edit text), or saving/loading canvas state.
      • The color picker currently only changes the fill color. You could add more properties (e.g., stroke color) by extending the Pickr change handler.
      • Error handling is minimal; check the browser console for font loading errors.
      • If you want to use a custom font not from Google Fonts, add an @font-face rule in the CSS and reference it in the font dropdown.
    • Dependencies: All libraries are loaded via CDNs, so an internet connection is required for the first load. For production, consider hosting the libraries locally or using a bundler like Webpack.

    This code should work out of the box in a modern browser. If you have any modifications or additional features in mind, let me know!

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