By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Canva drawing Javascript on elementor


New Coder
Hello everyone,

I wanted to implement an interactive drawing on my wordpress using the HTML element.
The code works well outside of wordpress but once implemented, the mouse is movement seems off ( it is far away from the cursor). Any idea why that is ?
Here is the page where i want to implement the canva on wordpress : Design me a sheep – Votre studio de Webdesign et de Graphisme.

Here is the code :

<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Manrope&display=swap" rel="stylesheet">
    <title>Drawing app</title>
*:after {
    box-sizing: border-box;

body {
    margin: 0;
    line-height: 1.5;
    font-family: 'Manrope', sans-serif;

    margin-left: 2em;
    height: 80%;
    width: 80%;

    display: flex;
    align-items: center;
    justify-content: space-around;


    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    width: 70px;
    height: 70px;
    background-color: transparent;
    border: none;
    cursor: pointer;

    border-radius: 15px;
    border: none;

    visibility: hidden;


    font-family: 'Manrope', sans-serif;
    border-radius: 4em;
    background-color: black;
    border: solid black 2px;
    padding-right: 2em;
    padding-top: .5em;
    padding-bottom: .5em;
    color: white;


    color: black;
    background-color: transparent;

    <section class="container">
        <div class="drawing-board">
            <canvas id="drawing-board"></canvas>
        <div id="toolbar">
            <label for="stroke">Choisissez votre couleur : </label>
            <input id="stroke" name='stroke' type="color" value="#43da86">
            <button id="clear">Effacer</button>
            <input id="lineWidth" name='lineWidth' type="number" value="18">

    const canvas = document.getElementById('drawing-board');
const toolbar = document.getElementById('toolbar');
const ctx = canvas.getContext('2d');

const canvasOffsetX = canvas.offsetLeft;
const canvasOffsetY = canvas.offsetTop;

canvas.width = window.innerWidth - canvasOffsetX;
canvas.height = window.innerHeight - canvasOffsetY;

let isPainting = false;
let lineWidth = 18;
let startX;
let startY;

toolbar.addEventListener('click', e => {
    if (e.target.id === 'clear') {
        ctx.clearRect(0, 0, canvas.width, canvas.height);

toolbar.addEventListener('change', e => {
    if(e.target.id === 'stroke') {
        ctx.strokeStyle = e.target.value;

    if(e.target.id === 'lineWidth') {
        lineWidth = e.target.value;

const draw = (e) => {
    if(!isPainting) {

    ctx.lineWidth = lineWidth;
    ctx.lineCap = 'round';

    ctx.lineTo(e.clientX - canvasOffsetX, e.clientY);

canvas.addEventListener('mousedown', (e) => {
    isPainting = true;
    startX = e.clientX;
    startY = e.clientY;

canvas.addEventListener('mouseup', e => {
    isPainting = false;

canvas.addEventListener('mousemove', draw);

Yes, your code works ok outside Wordpress. Inside, it looks like it's adding the width of the left panel and the height of the top panel to the canvas start offsets. Why this would be so, no idea - I don't know how Wordpress works. I think it will work for you when you change

const canvasOffsetX = canvas.offsetLeft;
const canvasOffsetY = canvas.offsetTop;
const canvasOffsetX = 0;
const canvasOffsetY = 0;
Thank you for your answer and for your reactivity. The code works on wordpress now,
That's good. So you made the change I suggested ? This also works in the non-Wordpress scenario, provided you take out this

margin-left: 2em;

otherwise drawing is always a little (2em) to the right of the cursor. Why this apparently isn't an issue in the Wordpress scenario, no idea...

but something I hadn't thought about is the opacity of the stroke: can I had it in the html input ?
I've been reading up on transparency, and found this ctx.globalAlpha property which you can set anywhere between zero and one. But while this works fine for filled shapes, it pertinently refuses to work for strokes. I don't understand as yet if that is by design or not.

Of course you can put that parameter value in an input element like you did for color and linewidth. But that's not much use if it doesn't work 😳

New Threads

Buy us a coffee!

Top Bottom