Search code examples
javascriptjspdf

jsPDF how to rotate a rectangle?


I am using jsPDF to create a PDF with javascript and I am stuck at one point where I have to rotate a rectangle.

I have defined my rectangle like this:

doc.setFillColor(255,255,200);
doc.rect(100, 0, 70, 70, 'F');

In the documentation I found under section context2d the rotate method which accepts one parameter which is the angle but that is all. I don't know how to use it or if this can be applied to the rectangle.


Solution

  • I think it can be done with something like this with context2d:

    context = doc.context2d;
    context.fillStyle = 'red';
    context.fillRect(545, 205, 50, 40);
    context.rotate(Math.PI/4)
    context.rect(5, 5, 150, 150);
    context.stroke();
    context.fill();
    

    or use lines or path and draw it manually.