Can't draw rectangle on existing <canvas> that renders PDF with pdfjs


Kiran

Task

<canvas id="pdfCanvas">Create an existing canvas in your web canvas The fabric canvas new fabric.Canvas("pdfCanvas");has mouse.down, mouse.up, mouse.move methods that make the drawn rectangle rendered on top of the canvas PDF "pdfCanvas" using PDF.js browsers now display the rendered PDF now in Draw rectangle on pdf, it hides rendered canvas but does draw objects

question

Here's a fiddle to see the problem: - run the above https://jsfiddle.net/hiitskiran/wgz8qore/2/ - you can see the fabric rectangle is hidden behind the rendered pdf - click on the pdf canvas area to see the fabric object

Alessandro

What I see from your fiddle is that you don't await the result of the method that is page.renderasync , then unfortunately if you do page.render, the fabric.canvas instance will clear your previously populated canvas and we can't do anything for that This. So I try another approach: first, I wait for the pdf.js page.rendermethod, then I call the .toDataURL that I did before filling the canvas, I can put a copy of the old content, then I put the new fabric.canvas you previously filled with the canvas background and Finish drawing the rectangle.

var url = 'http://www.africau.edu/images/default/sample.pdf';
var canvas = document.getElementById('pdfcanvas');
var context = canvas.getContext('2d');
PDFJS.disableWorker = true;
PDFJS.getDocument(url).then(function(pdf) {
  pdf.getPage(1).then(function(page) {
    var scale = 1.5;
    var viewport = page.getViewport(scale);
    canvas.height = viewport.height;
    canvas.width = viewport.width;
    page.render({
      canvasContext: context,
      viewport: viewport
    }).then(function() {

      var bg = canvas.toDataURL("image/png");
      var fcanvas = new fabric.Canvas("pdfcanvas", {
        selection: false
      });
   fcanvas.setBackgroundImage(bg,fcanvas.renderAll.bind(fcanvas));
      fcanvas.setWidth(300);
      fcanvas.setHeight(300);
      var rect = new fabric.Rect({
        left: 100,
        top: 100,
        width: 50,
        height: 50,
        fill: '#FF454F',
        opacity: 0.5,
        transparentCorners: true,
        borderColor: "gray",
        cornerColor: "gray",
        cornerSize: 5
      });
      fcanvas.add(rect);
      fcanvas.renderAll();
    });
  });
});
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script>
<script src="https://seikichi.github.io/tmp/PDFJS.0.8.715/pdf.min.js"></script>
<canvas id="pdfcanvas" style="border:1px solid black"></canvas>

Related


draw rectangle in canvas

125fur I can draw rectangle on video on canvas. But it's hidden behind the video but can't suggest how I should draw the rectangle on the video through the side.Pls like I can draw the code on the image. : <p>Video to use:</p> <video id="video1" controls wi

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Draw rectangle with loaded pdf file in canvas using pdf.js

Sharma Vikram I am trying to draw rectangles on a pdf file. When I draw rectangles in pdf, the rectangles are not drawn correctly. I want to draw only one rectangle at a time, when I draw the new one, the old one should be deleted, but this is not happening. H

draw rectangle in canvas

125fur I can draw rectangle on video on canvas. But it's hidden behind the video but can't suggest how I should draw the rectangle on the video through the side.Pls like I can draw the code on the image. : <p>Video to use:</p> <video id="video1" controls wi

Draw resizable rectangle on canvas

Inde · 穆里罗 (Masinde Muliro) I am trying to draw a resizable rectangle on a canvas element. I check the position of the mouse relative to the rectangle on the screen and then update the width/height of the triangle the mouse falls into. So far I've managed to s

I can't draw rectangle using canvas as child component

Riddle I need to draw a rectangle using canvas. Canvas must be in child component. Just like the updateCanvas function. Is it possible to do so? I am trying to draw a rectangle in the parent component and everything works fine, but in this case I need to put t

I can't draw rectangle using canvas as child component

Riddle I need to draw a rectangle using canvas. Canvas must be in child component. Just like the updateCanvas function. Is it possible to do so? I am trying to draw a rectangle in the parent component and everything works fine, but in this case I need to put t

draw rectangle on canvas

Avsana Khan I'm trying to develop a ping pong game in Android where I will have two bats to hit the ball. I drew a bat on the canvas and it was placed at the far left of the screen. I need another bat on the far right of the screen, but every time the paddle i

draw rectangle on canvas

Avsana Khan I'm trying to develop a ping pong game in Android where I will have two bats to hit the ball. I drew a bat on the canvas and it was placed at the far left of the screen. I need another bat on the far right of the screen, but every time the paddle i

Can't draw rectangle on JPanel

Jonah I'm currently trying to learn more about swing painting and am creating a little game but seem to be stuck. I'm trying to add several rectangles (enemies) to an ArrayList and then draw them in a paintComponent()method , but that doesn't seem to work. Thi

Draw rectangle with loaded pdf file in canvas using pdf.js

Sharma Vikram I am trying to draw rectangles on a pdf file. When I draw rectangles in pdf, the rectangles are not drawn correctly. I want to draw only one rectangle at a time, when I draw the new one, the old one should be deleted, but this is not happening. H

Tkinter won't draw rectangle on Canvas

Cadel Watson I'm trying to initialize a grid of blue rectangles with a user-specified size. However, the rectangle is not drawn on the initialized canvas. I am trying to store them in a matrix for later use. My code is as follows: import Tkinter import sys fro

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Draw rectangle with loaded pdf file in canvas using pdf.js

Sharma Vikram I am trying to draw rectangles on a pdf file. When I draw rectangles in pdf, the rectangles are not drawn correctly. I want to draw only one rectangle at a time, when I draw the new one, the old one should be deleted, but this is not happening. H

Can't rotate rectangle in canvas

Rajib I am new to HTML5 canvas. I'm trying to build a game area using an HTML canvas, where someone can drive around a car inside the Canvas area, like if I press the left arrow key, the car will turn to the left, and if the right arrow key is pressed, the car

Draw resizable rectangle on canvas

Inde · 穆里罗 (Masinde Muliro) I am trying to draw a resizable rectangle on a canvas element. I check the position of the mouse relative to the rectangle on the screen and then update the width/height of the triangle the mouse falls into. So far I've managed to s

draw rectangle in canvas

125fur I can draw rectangle on video on canvas. But it's hidden behind the video but can't suggest how I should draw the rectangle on the video through the side.Pls like I can draw the code on the image. : <p>Video to use:</p> <video id="video1" controls wi

Can't draw rectangle on JPanel

Jonah I'm currently trying to learn more about swing painting and am creating a little game but seem to be stuck. I'm trying to add several rectangles (enemies) to an ArrayList and then draw them in a paintComponent()method , but that doesn't seem to work. Thi

Draw rectangle with loaded pdf file in canvas using pdf.js

Sharma Vikram I am trying to draw rectangles on a pdf file. When I draw rectangles in pdf, the rectangles are not drawn correctly. I want to draw only one rectangle at a time, when I draw the new one, the old one should be deleted, but this is not happening. H

Draw rectangle with loaded pdf file in canvas using pdf.js

Sharma Vikram I am trying to draw rectangles on a pdf file. When I draw rectangles in pdf, the rectangles are not drawn correctly. I want to draw only one rectangle at a time, when I draw the new one, the old one should be deleted, but this is not happening. H

Tkinter won't draw rectangle on Canvas

Cadel Watson I'm trying to initialize a grid of blue rectangles with a user-specified size. However, the rectangle is not drawn on the initialized canvas. I am trying to store them in a matrix for later use. My code is as follows: import Tkinter import sys fro

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co