Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 521191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:13:28+00:00 2026-05-13T08:13:28+00:00

Let’s say that I got the following code in a page: elit ac <a

  • 0

Let’s say that I got the following code in a page:

elit ac <a href="http://www.ducksanddos/bh.php" id='link'>Hello world!</a> nunc

And that I want to make the <a> contain a <canvas> element which will display the exact same text and will look exactly like the <a> looked before the change, resulting code:

elit ac <a href="http://www.ducksanddos/bh.php" id='link'><canvas width='xxx' height='xxx' /></a> nunc

After investing some time I arrived to this partial solution (btw I also use jQuery):

var width   = $('#link').width();
var height  = $('#link').height();

var element = document.getElementById('link');
var content = element.innerHTML;

var fontfamily  = document.defaultView.getComputedStyle(element, null).getPropertyValue('font-family');
var fontsize  = document.defaultView.getComputedStyle(element, null).getPropertyValue('font-size');

// create the canvas element
var canvas      = doc.createElement("canvas");
canvas.width    = width;
canvas.height   = height;


var ctx = canvas.getContext('2d');
ctx.textBaseline = 'bottom';
ctx.font = fontsize+" "+fontfamily;
ctx.fillText(content, 0, 22);

element.innerHTML   = '';   // delete original content of <a>
element.appendChild(canvas);

}

My problem is that from some reason the text is displayed a little bit higher (few pixels) then the rest of the text which surround the <a> (that’s after I gave it an extra 22px at ctx.fillText(content, 0, 22); in order to make the text visible at all..).

When checking with the firebug selector it seems that the <canvas> element is the same size as the <a> but located a little bit higher (which is weird cause it’s a child element of it…).

One last thing, all of this code is being run from a Firefox extension so there is no need for compatibility with other browsers.

Thx a lot!
Yonatan Ben-Nes

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:13:28+00:00Added an answer on May 13, 2026 at 8:13 am

    Keep in mind that a rendered canvas is sort of like an image on the page. So it will follow layout rules as such (this means it may not align exactly with your text on it’s own)

    You may need to use CSS to align the resulting image element with your text. Try adding the following JS at the end of the above script:

    canvas.style.verticalAlign="middle";
    

    That seemed to do it for me when I tried your sample (I provided my own sample html etc). I also had to change the fillText command to:

    ctx.fillText("content", 0, 20);
    

    But that could have been because of my browser/OS font settings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 292k
  • Answers 292k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The old C library libmemcache.so had some serious bugs and… May 13, 2026 at 6:12 pm
  • Editorial Team
    Editorial Team added an answer Say var that = this after this.debug, then do that.debug. May 13, 2026 at 6:12 pm
  • Editorial Team
    Editorial Team added an answer select * from syslogins where pwdcompare (name,password)=1 BOL: pwdcompare May 13, 2026 at 6:12 pm

Related Questions

I have a French site that I want to parse, but am running into
Let's say you create a wizard in an HTML form. One button goes back,
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let me try to explain what I need. I have a server that is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.