How to do a drawing on screen?

Can anyone suggest some code or tutorial how to create very basic drawing. I don’t need more than one button to activate drawing, one button for blue, one button for red color, undo button or erase button. I asked chatGPT but it just says I need some API.

Sorry but I just dont fu… manage to manage the content in this small text area. I hope this text and code bellow is not too confusing. But I need the function for drawing or erasing. Any one can help with this?

manifest.json

{
  "manifest_version": 2,
  "name": "My Draw Extension",
  "version": "1.0",
  "description": "Extension for drawing on the screen",
  "icons": {
    "48": "icon.png"
  },
  "permissions": [
    "activeTab",
    "storage"
  ],
  "browser_action": {
    "default_icon": {
      "48": "icon.png"
    },
    "default_popup": "popup.html"
  },
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  }
}
<!DOCTYPE html>
<html>
<head>
  <title>My Draw Extension</title>
  <link rel="stylesheet" type="text/css" href="popup.css">
  <script src="popup.js"></script>
</head>
<body>
  <div class="container">
    <button id="redButton">Red</button>
    <button id="blueButton">Blue</button>
    <input type="range" id="thicknessSlider" min="1" max="10" value="1">
    <button id="undoButton">Undo</button>
    <button id="clearButton">Clear</button>
    <button id="eraserButton">Eraser</button>
  </div>
</body>
</html>

popup.css

css

.container {
  display: flex;
  flex-direction: row;
}

button {
  margin: 5px;
}
  1. “popup.js”
$(document).ready(function() {
  const redButton = $("#redButton");
  const blueButton = $("#blueButton");
  const thicknessSlider = $("#thicknessSlider");
  const undoButton = $("#undoButton");
  const clearButton = $("#clearButton");
  const eraserButton = $("#eraserButton");

  redButton.on("click", function() {
    // Logic for selecting red color
  });

  blueButton.on("click", function() {
    // Logic for selecting blue color
  });

  thicknessSlider.on("input", function() {
    // Logic for changing line thickness based on slider value
    const thickness = $(this).val();
    // Update line thickness
  });

  undoButton.on("click", function() {
    // Logic for undoing the last drawn line
  });

  clearButton.on("click", function() {
    // Logic for clearing the drawing canvas
  });

  eraserButton.on("click", function() {
    // Logic for activating the eraser mode
  });

  // ...
});


1. To file `popup.js` you should add the function to capturing the functions for drawing:

javascript

    let isDrawing = false;
    let previousX = 0;
    let previousY = 0;

    $(document).ready(function() {
      // ...

      // mousedown events
      $(document).on("mousedown", function(e) {
        isDrawing = true;
        previousX = e.pageX;
        previousY = e.pageY;
      });

      // mousemove events
      $(document).on("mousemove", function(e) {
        if (isDrawing) {
          // implement logic for drawing
          // based on previousX, previousY and current positions e.pageX, e.pageY
          // U can use API like <canvas> or SVG.
        }
        previousX = e.pageX;
        previousY = e.pageY;
      });

      // Add the events  for mouse button release - end of drawing
      $(document).on("mouseup", function() {
        isDrawing = false;
      });

      // ...
    });
```

popup.html

<!DOCTYPE html>
<html>
<head>
  <title>My Draw Extension</title>
  <link rel="stylesheet" type="text/css" href="popup.css">
  <script src="jquery.min.js"></script>
  <script src="popup.js"></script>
</head>
<body>
  <!-- ... -->
</body>
</html>

popup.css
.container {
display: flex;
flex-direction: row;
}

button {
  margin: 5px;
}

**popup.js**
```
$(document).ready(function() {
  const redButton = $("#redButton");
  const blueButton = $("#blueButton");
  const thicknessSlider = $("#thicknessSlider");
  const undoButton = $("#undoButton");
  const clearButton = $("#clearButton");
  const eraserButton = $("#eraserButton");

  redButton.on("click", function() {
    // change the color to red
  });

  // the rest of the code
});
```

You can’t ask AI to build complex things like this yet.

Maybe check existing drawing projects, for example:


The second one is interesting. Do you know what is the licence? Can I edit it for my purposes? Or maybe I could send it to AI to find out function which I need… And to get rid of the redundant stuff from the library. But still here I have a problem with the AI because it does not accept big files. I asked it and it told me that it can accept messages up to 4KB and/or 60 000 tokens. But IDK what it means by token, if it means that one token is 4KB message or there is bigger limit for sending a message if I would send it from my own app.

Do you think AI will be able to do this someday?
Or can we look forward to a future where AI writes programs, and humans fix the AI’s mistakes?

@h4ever sadly I can’t answer any of your questions :frowning:

@hans_squared yes, I’m 100% sure, in not so distant future, you will be able ask your chatbot anything and it will send you .xpi file or a link to new github repo of a finished addon :frowning:.
I’m already using it for development and it’s super scary useful (sometimes it makes me feel like junior programmer again).

1 Like

I suspect they gave it a name “AI” but it is not AI at all, but just NLP. It’s just so stupid. How could it be even called AI, when it does not have memory to remember previous interactions with humans? I mean for example, there is filemanager called fff. I got this recommendation for using fff recent days by this OpenAI. I asked it for help how to display hidden files, but it just cannot find out what is fff. It connected it somehow with Windows apps. I found help doc on github and sent it to it. It almost found it as the hotkey to be written .: the “AI” interpreted it and read it as alt+. . But reagarding writing scripts, I just hope it will be more predictible, because now it generate dozens times different result when I asked it to extract some information which it was trained on. The communication must improve and I hope, the developers could have some more official language like using some supported marks to better communicate with it using exact term like for example to structure and to describe my problem using marks similar to html or xml. And the memory must improve to memory exact conditions, arguments and rule I gave it.

That’s funny :slight_smile: