Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Rotate text

Rotate text

Scheduled Pinned Locked Moved JavaScript
javascripttoolstutorial
3 Posts 2 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rockallica
    wrote on last edited by
    #1

    Hi, New here. Anyway I'm working on a project and I'm placing some text over some images. All is good with that. However I only want to rotate the text 35 degrees but I can't figure how to do that in Javascript. Here is how I'm displaying the text -

    const ctxt = canvas.getContext('2d');
    ctxt.fillStyle = 'red';
    ctxt.font = '40px Brush Script MT';
    ctxt.fillText('TEST',200,178);

    Thanks.

    G 1 Reply Last reply
    0
    • R Rockallica

      Hi, New here. Anyway I'm working on a project and I'm placing some text over some images. All is good with that. However I only want to rotate the text 35 degrees but I can't figure how to do that in Javascript. Here is how I'm displaying the text -

      const ctxt = canvas.getContext('2d');
      ctxt.fillStyle = 'red';
      ctxt.font = '40px Brush Script MT';
      ctxt.fillText('TEST',200,178);

      Thanks.

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      The canvas uses a transformation matrix and it has a few functions for manipulating it. Here is a 35-degree rotation:

      const ctxt = canvas.getContext('2d');
      ctxt.fillStyle = 'red';
      ctxt.font = '40px Brush Script MT';
      const tm = ctxt.measureText('TEST');
      ctxt.translate(200 + tm.width / 2, 178);
      ctxt.rotate(35 * 3.14159 / 180);
      ctxt.translate(-(200 + tm.width / 2), -178);
      ctxt.fillText('TEST',200,178);
      ctxt.resetTransform();

      R 1 Reply Last reply
      0
      • G Graham Breach

        The canvas uses a transformation matrix and it has a few functions for manipulating it. Here is a 35-degree rotation:

        const ctxt = canvas.getContext('2d');
        ctxt.fillStyle = 'red';
        ctxt.font = '40px Brush Script MT';
        const tm = ctxt.measureText('TEST');
        ctxt.translate(200 + tm.width / 2, 178);
        ctxt.rotate(35 * 3.14159 / 180);
        ctxt.translate(-(200 + tm.width / 2), -178);
        ctxt.fillText('TEST',200,178);
        ctxt.resetTransform();

        R Offline
        R Offline
        Rockallica
        wrote on last edited by
        #3

        Awesome! Just what I needed. Thanks!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups