canvas - how to RESET rectangle area when creating new on image? The 2019 Stack Overflow Developer Survey Results Are InHow to draw a rounded Rectangle on HTML Canvas?How to clear the canvas for redrawingResizing an image in an HTML5 canvasHow to produce such page flip effects in Canvas?How to add image to canvasDisable Interpolation when Scaling a <canvas>Canvas and SharePoint dropdown menu conflict in ChromeHow To Save Canvas As An Image With canvas.toDataURL()?JS Boolean var doesn't affect if-statementsHow to fix this “mouse hover” function in a canvas

What's the name of these plastic connectors

Can you cast a spell on someone in the Ethereal Plane, if you are on the Material Plane and have the True Seeing spell active?

Is bread bad for ducks?

Slides for 30 min~1 hr Skype tenure track application interview

How do you keep chess fun when your opponent constantly beats you?

What is this business jet?

Relationship between Gromov-Witten and Taubes' Gromov invariant

Ubuntu Server install with full GUI

Inverse Relationship Between Precision and Recall

Button changing its text & action. Good or terrible?

How to translate "being like"?

What is the most efficient way to store a numeric range?

Match Roman Numerals

Does adding complexity mean a more secure cipher?

"as much details as you can remember"

Keeping a retro style to sci-fi spaceships?

Why “相同意思的词” is called “同义词” instead of "同意词"?

For what reasons would an animal species NOT cross a *horizontal* land bridge?

Can I have a signal generator on while it's not connected?

Cooking pasta in a water boiler

How to notate time signature switching consistently every measure

A word that means fill it to the required quantity

Likelihood that a superbug or lethal virus could come from a landfill

Geography at the pixel level



canvas - how to RESET rectangle area when creating new on image?



The 2019 Stack Overflow Developer Survey Results Are InHow to draw a rounded Rectangle on HTML Canvas?How to clear the canvas for redrawingResizing an image in an HTML5 canvasHow to produce such page flip effects in Canvas?How to add image to canvasDisable Interpolation when Scaling a <canvas>Canvas and SharePoint dropdown menu conflict in ChromeHow To Save Canvas As An Image With canvas.toDataURL()?JS Boolean var doesn't affect if-statementsHow to fix this “mouse hover” function in a canvas



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















Can anyone help me for this please ?



Main requirement : How to clear previous rectangle areas when creating new one on image?



If above requirement resolved then it will be great, other than that is it possible to do following ?



a) Is it possible to resize rectangle after drawing it?



b) Is it possible to move rectangle possiton which was created






"use strict";
requestAnimationFrame(mainLoop);
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
const storedRects = [];
const baseImage = loadImage("https://www.w3schools.com/css/img_fjords.jpg");
var refresh = true;
const rect = (() =>
var x1, y1, x2, y2;
var show = false;
function fix()
rect.x = Math.min(x1, x2);
rect.y = Math.min(y1, y2);
rect.w = Math.max(x1, x2) - Math.min(x1, x2);
rect.h = Math.max(y1, y2) - Math.min(y1, y2);


function draw(ctx)
ctx.strokeRect(this.x, this.y, this.w, this.h)

const rect = x : 0, y : 0, w : 0, h : 0, draw;
const API =
restart(point)
x2 = x1 = point.x;
y2 = y1 = point.y;
fix();
show = true;
,
update(point)
x2 = point.x;
y2 = point.y;
fix();
show = true;
,
toRect()
show = false;
return Object.assign(, rect);
,
draw(ctx)
if (show) rect.draw(ctx)
,
show : false,

return API;
)();

function loadImage(url)
const image = new Image();
image.src = url;
image.onload = () => refresh = true;
return image;


const mouse =
button : false,
x : 0,
y : 0,
down : false,
up : false,
element : null,
event(e)
const m = mouse;
m.bounds = m.element.getBoundingClientRect();
m.x = e.pageX - m.bounds.left - scrollX;
m.y = e.pageY - m.bounds.top - scrollY;
const prevButton = m.button;
m.button = e.type === "mousedown" ? true : e.type === "mouseup" ? false : mouse.button;
if (!prevButton && m.button) m.down = true
if (prevButton && !m.button) m.up = true
,
start(element)
mouse.element = element;
"down,up,move".split(",").forEach(name => document.addEventListener("mouse" + name, mouse.event));



mouse.start(canvas);
function draw()
ctx.drawImage(baseImage, 0, 0, ctx.canvas.width, ctx.canvas.width);
ctx.lineWidth = 1;
ctx.strokeStyle = "yellow";
storedRects.forEach(rect => rect.draw(ctx));
ctx.strokeStyle = "red";
rect.draw(ctx);

function mainLoop()
if (refresh

<canvas id="myCanvas" width="800" height="500" title = "click and drag to add rectangles" style="border:1px solid #000000;cursor:crosshair"></canvas>












share|improve this question




























    0















    Can anyone help me for this please ?



    Main requirement : How to clear previous rectangle areas when creating new one on image?



    If above requirement resolved then it will be great, other than that is it possible to do following ?



    a) Is it possible to resize rectangle after drawing it?



    b) Is it possible to move rectangle possiton which was created






    "use strict";
    requestAnimationFrame(mainLoop);
    const canvas = document.getElementById("myCanvas");
    const ctx = canvas.getContext("2d");
    const storedRects = [];
    const baseImage = loadImage("https://www.w3schools.com/css/img_fjords.jpg");
    var refresh = true;
    const rect = (() =>
    var x1, y1, x2, y2;
    var show = false;
    function fix()
    rect.x = Math.min(x1, x2);
    rect.y = Math.min(y1, y2);
    rect.w = Math.max(x1, x2) - Math.min(x1, x2);
    rect.h = Math.max(y1, y2) - Math.min(y1, y2);


    function draw(ctx)
    ctx.strokeRect(this.x, this.y, this.w, this.h)

    const rect = x : 0, y : 0, w : 0, h : 0, draw;
    const API =
    restart(point)
    x2 = x1 = point.x;
    y2 = y1 = point.y;
    fix();
    show = true;
    ,
    update(point)
    x2 = point.x;
    y2 = point.y;
    fix();
    show = true;
    ,
    toRect()
    show = false;
    return Object.assign(, rect);
    ,
    draw(ctx)
    if (show) rect.draw(ctx)
    ,
    show : false,

    return API;
    )();

    function loadImage(url)
    const image = new Image();
    image.src = url;
    image.onload = () => refresh = true;
    return image;


    const mouse =
    button : false,
    x : 0,
    y : 0,
    down : false,
    up : false,
    element : null,
    event(e)
    const m = mouse;
    m.bounds = m.element.getBoundingClientRect();
    m.x = e.pageX - m.bounds.left - scrollX;
    m.y = e.pageY - m.bounds.top - scrollY;
    const prevButton = m.button;
    m.button = e.type === "mousedown" ? true : e.type === "mouseup" ? false : mouse.button;
    if (!prevButton && m.button) m.down = true
    if (prevButton && !m.button) m.up = true
    ,
    start(element)
    mouse.element = element;
    "down,up,move".split(",").forEach(name => document.addEventListener("mouse" + name, mouse.event));



    mouse.start(canvas);
    function draw()
    ctx.drawImage(baseImage, 0, 0, ctx.canvas.width, ctx.canvas.width);
    ctx.lineWidth = 1;
    ctx.strokeStyle = "yellow";
    storedRects.forEach(rect => rect.draw(ctx));
    ctx.strokeStyle = "red";
    rect.draw(ctx);

    function mainLoop()
    if (refresh

    <canvas id="myCanvas" width="800" height="500" title = "click and drag to add rectangles" style="border:1px solid #000000;cursor:crosshair"></canvas>












    share|improve this question
























      0












      0








      0








      Can anyone help me for this please ?



      Main requirement : How to clear previous rectangle areas when creating new one on image?



      If above requirement resolved then it will be great, other than that is it possible to do following ?



      a) Is it possible to resize rectangle after drawing it?



      b) Is it possible to move rectangle possiton which was created






      "use strict";
      requestAnimationFrame(mainLoop);
      const canvas = document.getElementById("myCanvas");
      const ctx = canvas.getContext("2d");
      const storedRects = [];
      const baseImage = loadImage("https://www.w3schools.com/css/img_fjords.jpg");
      var refresh = true;
      const rect = (() =>
      var x1, y1, x2, y2;
      var show = false;
      function fix()
      rect.x = Math.min(x1, x2);
      rect.y = Math.min(y1, y2);
      rect.w = Math.max(x1, x2) - Math.min(x1, x2);
      rect.h = Math.max(y1, y2) - Math.min(y1, y2);


      function draw(ctx)
      ctx.strokeRect(this.x, this.y, this.w, this.h)

      const rect = x : 0, y : 0, w : 0, h : 0, draw;
      const API =
      restart(point)
      x2 = x1 = point.x;
      y2 = y1 = point.y;
      fix();
      show = true;
      ,
      update(point)
      x2 = point.x;
      y2 = point.y;
      fix();
      show = true;
      ,
      toRect()
      show = false;
      return Object.assign(, rect);
      ,
      draw(ctx)
      if (show) rect.draw(ctx)
      ,
      show : false,

      return API;
      )();

      function loadImage(url)
      const image = new Image();
      image.src = url;
      image.onload = () => refresh = true;
      return image;


      const mouse =
      button : false,
      x : 0,
      y : 0,
      down : false,
      up : false,
      element : null,
      event(e)
      const m = mouse;
      m.bounds = m.element.getBoundingClientRect();
      m.x = e.pageX - m.bounds.left - scrollX;
      m.y = e.pageY - m.bounds.top - scrollY;
      const prevButton = m.button;
      m.button = e.type === "mousedown" ? true : e.type === "mouseup" ? false : mouse.button;
      if (!prevButton && m.button) m.down = true
      if (prevButton && !m.button) m.up = true
      ,
      start(element)
      mouse.element = element;
      "down,up,move".split(",").forEach(name => document.addEventListener("mouse" + name, mouse.event));



      mouse.start(canvas);
      function draw()
      ctx.drawImage(baseImage, 0, 0, ctx.canvas.width, ctx.canvas.width);
      ctx.lineWidth = 1;
      ctx.strokeStyle = "yellow";
      storedRects.forEach(rect => rect.draw(ctx));
      ctx.strokeStyle = "red";
      rect.draw(ctx);

      function mainLoop()
      if (refresh

      <canvas id="myCanvas" width="800" height="500" title = "click and drag to add rectangles" style="border:1px solid #000000;cursor:crosshair"></canvas>












      share|improve this question














      Can anyone help me for this please ?



      Main requirement : How to clear previous rectangle areas when creating new one on image?



      If above requirement resolved then it will be great, other than that is it possible to do following ?



      a) Is it possible to resize rectangle after drawing it?



      b) Is it possible to move rectangle possiton which was created






      "use strict";
      requestAnimationFrame(mainLoop);
      const canvas = document.getElementById("myCanvas");
      const ctx = canvas.getContext("2d");
      const storedRects = [];
      const baseImage = loadImage("https://www.w3schools.com/css/img_fjords.jpg");
      var refresh = true;
      const rect = (() =>
      var x1, y1, x2, y2;
      var show = false;
      function fix()
      rect.x = Math.min(x1, x2);
      rect.y = Math.min(y1, y2);
      rect.w = Math.max(x1, x2) - Math.min(x1, x2);
      rect.h = Math.max(y1, y2) - Math.min(y1, y2);


      function draw(ctx)
      ctx.strokeRect(this.x, this.y, this.w, this.h)

      const rect = x : 0, y : 0, w : 0, h : 0, draw;
      const API =
      restart(point)
      x2 = x1 = point.x;
      y2 = y1 = point.y;
      fix();
      show = true;
      ,
      update(point)
      x2 = point.x;
      y2 = point.y;
      fix();
      show = true;
      ,
      toRect()
      show = false;
      return Object.assign(, rect);
      ,
      draw(ctx)
      if (show) rect.draw(ctx)
      ,
      show : false,

      return API;
      )();

      function loadImage(url)
      const image = new Image();
      image.src = url;
      image.onload = () => refresh = true;
      return image;


      const mouse =
      button : false,
      x : 0,
      y : 0,
      down : false,
      up : false,
      element : null,
      event(e)
      const m = mouse;
      m.bounds = m.element.getBoundingClientRect();
      m.x = e.pageX - m.bounds.left - scrollX;
      m.y = e.pageY - m.bounds.top - scrollY;
      const prevButton = m.button;
      m.button = e.type === "mousedown" ? true : e.type === "mouseup" ? false : mouse.button;
      if (!prevButton && m.button) m.down = true
      if (prevButton && !m.button) m.up = true
      ,
      start(element)
      mouse.element = element;
      "down,up,move".split(",").forEach(name => document.addEventListener("mouse" + name, mouse.event));



      mouse.start(canvas);
      function draw()
      ctx.drawImage(baseImage, 0, 0, ctx.canvas.width, ctx.canvas.width);
      ctx.lineWidth = 1;
      ctx.strokeStyle = "yellow";
      storedRects.forEach(rect => rect.draw(ctx));
      ctx.strokeStyle = "red";
      rect.draw(ctx);

      function mainLoop()
      if (refresh

      <canvas id="myCanvas" width="800" height="500" title = "click and drag to add rectangles" style="border:1px solid #000000;cursor:crosshair"></canvas>








      "use strict";
      requestAnimationFrame(mainLoop);
      const canvas = document.getElementById("myCanvas");
      const ctx = canvas.getContext("2d");
      const storedRects = [];
      const baseImage = loadImage("https://www.w3schools.com/css/img_fjords.jpg");
      var refresh = true;
      const rect = (() =>
      var x1, y1, x2, y2;
      var show = false;
      function fix()
      rect.x = Math.min(x1, x2);
      rect.y = Math.min(y1, y2);
      rect.w = Math.max(x1, x2) - Math.min(x1, x2);
      rect.h = Math.max(y1, y2) - Math.min(y1, y2);


      function draw(ctx)
      ctx.strokeRect(this.x, this.y, this.w, this.h)

      const rect = x : 0, y : 0, w : 0, h : 0, draw;
      const API =
      restart(point)
      x2 = x1 = point.x;
      y2 = y1 = point.y;
      fix();
      show = true;
      ,
      update(point)
      x2 = point.x;
      y2 = point.y;
      fix();
      show = true;
      ,
      toRect()
      show = false;
      return Object.assign(, rect);
      ,
      draw(ctx)
      if (show) rect.draw(ctx)
      ,
      show : false,

      return API;
      )();

      function loadImage(url)
      const image = new Image();
      image.src = url;
      image.onload = () => refresh = true;
      return image;


      const mouse =
      button : false,
      x : 0,
      y : 0,
      down : false,
      up : false,
      element : null,
      event(e)
      const m = mouse;
      m.bounds = m.element.getBoundingClientRect();
      m.x = e.pageX - m.bounds.left - scrollX;
      m.y = e.pageY - m.bounds.top - scrollY;
      const prevButton = m.button;
      m.button = e.type === "mousedown" ? true : e.type === "mouseup" ? false : mouse.button;
      if (!prevButton && m.button) m.down = true
      if (prevButton && !m.button) m.up = true
      ,
      start(element)
      mouse.element = element;
      "down,up,move".split(",").forEach(name => document.addEventListener("mouse" + name, mouse.event));



      mouse.start(canvas);
      function draw()
      ctx.drawImage(baseImage, 0, 0, ctx.canvas.width, ctx.canvas.width);
      ctx.lineWidth = 1;
      ctx.strokeStyle = "yellow";
      storedRects.forEach(rect => rect.draw(ctx));
      ctx.strokeStyle = "red";
      rect.draw(ctx);

      function mainLoop()
      if (refresh

      <canvas id="myCanvas" width="800" height="500" title = "click and drag to add rectangles" style="border:1px solid #000000;cursor:crosshair"></canvas>





      "use strict";
      requestAnimationFrame(mainLoop);
      const canvas = document.getElementById("myCanvas");
      const ctx = canvas.getContext("2d");
      const storedRects = [];
      const baseImage = loadImage("https://www.w3schools.com/css/img_fjords.jpg");
      var refresh = true;
      const rect = (() =>
      var x1, y1, x2, y2;
      var show = false;
      function fix()
      rect.x = Math.min(x1, x2);
      rect.y = Math.min(y1, y2);
      rect.w = Math.max(x1, x2) - Math.min(x1, x2);
      rect.h = Math.max(y1, y2) - Math.min(y1, y2);


      function draw(ctx)
      ctx.strokeRect(this.x, this.y, this.w, this.h)

      const rect = x : 0, y : 0, w : 0, h : 0, draw;
      const API =
      restart(point)
      x2 = x1 = point.x;
      y2 = y1 = point.y;
      fix();
      show = true;
      ,
      update(point)
      x2 = point.x;
      y2 = point.y;
      fix();
      show = true;
      ,
      toRect()
      show = false;
      return Object.assign(, rect);
      ,
      draw(ctx)
      if (show) rect.draw(ctx)
      ,
      show : false,

      return API;
      )();

      function loadImage(url)
      const image = new Image();
      image.src = url;
      image.onload = () => refresh = true;
      return image;


      const mouse =
      button : false,
      x : 0,
      y : 0,
      down : false,
      up : false,
      element : null,
      event(e)
      const m = mouse;
      m.bounds = m.element.getBoundingClientRect();
      m.x = e.pageX - m.bounds.left - scrollX;
      m.y = e.pageY - m.bounds.top - scrollY;
      const prevButton = m.button;
      m.button = e.type === "mousedown" ? true : e.type === "mouseup" ? false : mouse.button;
      if (!prevButton && m.button) m.down = true
      if (prevButton && !m.button) m.up = true
      ,
      start(element)
      mouse.element = element;
      "down,up,move".split(",").forEach(name => document.addEventListener("mouse" + name, mouse.event));



      mouse.start(canvas);
      function draw()
      ctx.drawImage(baseImage, 0, 0, ctx.canvas.width, ctx.canvas.width);
      ctx.lineWidth = 1;
      ctx.strokeStyle = "yellow";
      storedRects.forEach(rect => rect.draw(ctx));
      ctx.strokeStyle = "red";
      rect.draw(ctx);

      function mainLoop()
      if (refresh

      <canvas id="myCanvas" width="800" height="500" title = "click and drag to add rectangles" style="border:1px solid #000000;cursor:crosshair"></canvas>






      html5 canvas






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 10:49









      ViralViral

      199116




      199116






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55061605%2fcanvas-how-to-reset-rectangle-area-when-creating-new-on-image%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55061605%2fcanvas-how-to-reset-rectangle-area-when-creating-new-on-image%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

          Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

          Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved