Login | Register | FAQ
Anonymous

Referencing canvas inside a function's scope

+ Post a reply

13 posts 1, 2


Referencing canvas inside a function's scope

by greg79 » Wed Dec 13, 2017 6:25 pm

I get error with this code, it seems like ctx is not known inside the function. How can I reference the canvas from within the function?


Code: Select all
var c = document.getElementById("canvas1");
var ctx = c.getContext("2d");

var isDrawing = false;
var mX, mY, rX, rY;

 
function InitThis() {


    c.onmousedown = function(e) {
      isDrawing = true;
      mX = e.clientX;
      mY = e.clientY;
      };

    c.onmousemove = function(e) {
        if (isDrawing) {
          rX = e.clientX;
          rY = e.clientY;
          draw();
          ctx.clearRect(0, 0, canvas.width, canvas.height);
        }
    };

    c.onmouseup = function(e) {
      rX = e.clientX;
      rY = e.clientY;
      isDrawing = false;
  }

}

function draw() {
   
    ctx.beginPath();
    ctx.moveTo(mX,mY);
    ctx.lineTo(rX, rY);
    ctx.closePath();
    ctx.stroke();

}

InitThis()



The error: "ReferenceError: canvas is not defined
at HTMLCanvasElement.InitThis.c.onmousemove (zivuqeyure.js:22:31)"



Any idea how can I fix this?
User avatar

greg79

  • Posts: 1
  • Joined: Wed Dec 13, 2017 6:20 pm

Re: Referencing canvas inside a function's scope

by taynguyenem » Mon Feb 05, 2018 3:33 pm

Hi
You need add link to js of canvas.
Ex: https://cdnjs.cloudflare.com/ajax/libs/ ... anvasjs.js
Hope it help to you.
User avatar

taynguyenem

  • Posts: 30
  • Joined: Mon Feb 05, 2018 3:28 pm
--
Preview site only Html: teamviewdownload.com, winrarfree.net

Re: Referencing canvas inside a function's scope

by casseverhart13 » Tue Aug 13, 2019 9:18 am

great post here. . . thank you! tree trimming
User avatar

casseverhart13

  • Posts: 48
  • Joined: Tue Apr 23, 2019 4:22 am

Re: Referencing canvas inside a function's scope

by Danielperez123 » Tue Feb 25, 2020 5:20 pm

Wooah! this type of scope is really amazing. Thanks for sharing | High Risk Merchant account
User avatar

Danielperez123

  • Posts: 34
  • Joined: Fri May 24, 2019 8:14 pm

Re: Referencing canvas inside a function's scope

by ob82354 » Tue Apr 28, 2020 1:41 pm

Thanks for posting this!

Olivia| chimney sweeper
User avatar

ob82354

  • Posts: 1
  • Joined: Fri Apr 17, 2020 1:56 pm

Re: Referencing canvas inside a function's scope

by toppostguns » Tue May 05, 2020 12:26 pm

Out of all the JavaScript YouTube tutorials I've watched, yours are the best! Tree Removal Service
User avatar

toppostguns

  • Posts: 1
  • Joined: Tue May 05, 2020 12:20 pm

Re: Referencing canvas inside a function's scope

by johncowell » Fri Jun 19, 2020 4:50 pm

Did you add the link to the js of the canvas?

dog obedience training cheyenne monument maker boston ma custom cabinet makers in boston sink reglazing boston product management resume

Last edited by johncowell on Sat Jun 27, 2020 3:48 am, edited 1 time in total.

User avatar

johncowell

  • Posts: 1
  • Joined: Fri Jun 19, 2020 3:41 pm

Re: Referencing canvas inside a function's scope

by TIMH2019 » Thu Jun 25, 2020 8:17 pm



I would assume this is Java Script.

Tim - https://www.garagedoorIL.com
User avatar

TIMH2019

  • Posts: 1
  • Joined: Thu Jan 23, 2020 5:54 pm

Re: Referencing canvas inside a function's scope

by manchutseng363 » Wed Jul 15, 2020 5:10 am

This code is authentic and straight to the point. Keep up the good work.
https://fencebuilderproschandler.com/
User avatar

manchutseng363

  • Posts: 1
  • Joined: Wed Jul 15, 2020 4:10 am

Re: Referencing canvas inside a function's scope

by joeyG101 » Sat Aug 01, 2020 2:51 pm

Thank you for the quick and concise post. This code looks good!
https://www.kalispellroofers.com/
https://www.kalispellcleaningservice.com/
https://www.kalispellwindowcleanings.com/
Thanks again.
User avatar

joeyG101

  • Posts: 1
  • Joined: Sat Aug 01, 2020 2:42 pm


+ Post a reply

1, 2