Canvas drawing HTML Javascript on elementor

Feb 22, 2023
Reaction score
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 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="">
<link rel="preconnect" href="" crossorigin>
<link href="" 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 ( === 'clear') {
        ctx.clearRect(0, 0, canvas.width, canvas.height);

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

    if( === 'lineWidth') {
        lineWidth =;

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);

Mar 5, 2023
Reaction score
The issue with the interactive drawing on your WordPress page is likely due to the way WordPress modifies the HTML and CSS of your page. One possible reason for the issue is that the canvas element is not positioned correctly within the WordPress page, which causes the mouse coordinates to be offset. Here are a few suggestions you can try to fix the issue:

  1. Verify the HTML structure of your WordPress page: Make sure the HTML structure of your WordPress page matches the structure of the HTML document where the drawing app is working correctly. Check for any differences in the parent elements of the canvas element, as well as any added or modified CSS styles that could be affecting the position of the canvas.
  2. Check for conflicts with other JavaScript or CSS: Make sure there are no conflicts with other JavaScript or CSS on your WordPress page that could be affecting the position of the canvas element or its interaction with the mouse events.
  3. Use absolute positioning for the canvas element: You can try adding a CSS style to the canvas element to position it absolutely within its parent element. For example, you can add the following styles to the .drawing-board selector:

.drawing-board {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;

This will ensure the canvas element is positioned correctly within its parent element, regardless of any modifications to the page's HTML or CSS.

  1. Use the getBoundingClientRect() method to calculate the canvas offset: You can also try using the getBoundingClientRect() method to calculate the offset of the canvas element relative to the viewport. This method returns the size of an element and its position relative to the viewport. Here's an example of how you can use it:
const rect = canvas.getBoundingClientRect();
const canvasOffsetX = rect.left;
const canvasOffsetY =;

This should ensure the correct offset for the mouse events.

I hope these suggestions help you fix the issue with your interactive drawing app on your WordPress page.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Latest member

Latest Threads