Using HTML5 canvas.toDataURL to generate placeholder images

A while ago lewis had a great idea – an online tool to figure out the proportional height, when given a width and a particular proportion.

I’ve actually needed to use it 3 or 4 times since it was created around a month ago, so I was keen to get involved and implement some of the suggestions he received to make it better. One of the suggestions was that the tool should generate placeholder graphics, which developers can use whilst creating websites.

I was thinking PHP GD library, which could have quite easily have done the job. However, there are a few disadvantages with this:

  1. The user has to wait while the browser makes a request to the server and gets the response
  2. The server has to have the GD image library installed!
  3. The server has to do work to generate images – eating up your valuable CPU cycles
  4. The server has to temporarily store the generated image – using up disk space
  5. The request and response both use up bandwidth
  6. The tool wouldn’t work offline

All of which could be argued to be relatively inconsequential…but I wanted to do something fun. I decided to use the HTML5 canvas element to draw the placeholder graphic because, well, that sounded fun.

Canvas has a method called "toDataURL" which (as you can probably guess) encodes the content of the canvas as a data url. I spent a couple of hours in the evening knocking up a prototype utilising this functionality.

You may be thinking (when looking at the source for the prototype): "where’s the canvas"? What actually happens is that when you change the width or height using the input boxes, the page dynamically creates a canvas element, draws the placeholder graphic inside, calls toDataURL and then creates an <img> tag, whose "src" attribute is set the data url we got from the canvas. The page then removes the canvas element and the old placeholder graphic, and appends the new graphic to the DOM. The process is so fast you don’t ever see the canvas element on the page.

The reason we don’t just leave the canvas on the page is because we want users to be able to save these graphics. You can’t right click on a canvas and select "Save image as…".

The only disadvantage I can think of by using this method is that it won’t work on older browsers…or browsers without javascript enabled. Ok that is two. Other than that, it’s great:

  1. The user doesn’t have to wait for a round trip to the server to complete
  2. The server does no further work other than to serve the script file
  3. No bandwidth is consumed transferring the image
  4. The tool even works offline!

To me, that sounds like WIN.

15 thoughts on “Using HTML5 canvas.toDataURL to generate placeholder images

  1. Thanks a lot!
    I’ve been looking for an alternative to placeholder services like lorempixel.com

  2. As a Newbie, I am always searching online for articles that can help me. Thank you
    My website: anilingus.tv

  3. 2022 Oct; 29 10 6163 6188 what is priligy dapoxetine Antidiabetic therapy needs re evaluation when BSL falls beneath 56 mmol liter 100 mg dl and modification of antidiabetic therapy is essential if BSL drops What Causes Low Blood Sugar blood sugar won t come down beneath three9 mmol liter 70 mg dl If blood sugar monitors at walmart your blood sugar ranges are too excessive you can experience symptoms like frequent urination and elevated thirst Left untreated hyperglycemia can turn into diabetic ketoacidosis which happens when your physique is not ready to use insulin to interrupt blood sugar won t come down Blood Sugar Levels Chart down glucose for power HbA1c calculator calculates common plasma and whole blood glucose levels

  4. RocketPlay überzeugt mit einer abwechslungsreichen Spielauswahl
    für jedes Spielniveau. In nur wenigen Schritten startest du dein Casino Abenteuer online.
    Letztendlich kann man sich bei RocketPlay als Spieler davon überzeugen, dass man es mit einem fairen und verantwortungsbewussten Glücksspielbetreiber zu tun hat.
    Diese Plattform wurde geschaffen, um all unsere Bemühungen einer breiten Öffentlichkeit zu präsentieren, damit unsere Vision einer sichereren und
    transparenteren Online-Glücksspielbranche auch
    in die Realität umgesetzt wird. Diese Initiative haben wir mt dem Ziel gestartet, ein globales Selbstausschlusssystem zu schaffen, das es gefährdeten Spielern ermöglicht, ihren Zugang zu allen Online-Glücksspielmöglichkeiten global
    zu sperren. Der Spieler aus Deutschland hatte 1000 € eingezahlt
    und gewonnen, hatte jedoch Probleme mit der Auszahlung,
    da seine Gewinne als Bonusgelder eingestuft wurden, obwohl
    er den Nachweis hatte, ohne Bonus gespielt zu haben.
    Freispiele für Video-Slots werden an Neu- und Stammkunden vergeben. Es gibt Modelle von mehreren Dutzend
    Entwicklern von Software für Online-Glücksspiele,
    und die Liste der Marken wird ständig ergänzt.
    Ja, unser Casino operiert unter einer gültigen Curacao-Glücksspiellizenz und bietet legales Online-Glücksspiel für deutsche und
    österreichische Spieler. Nutzen Sie unseren großzügigen Willkommensbonus und beginnen Sie Ihre Reise in die aufregende Welt des
    Online-Glücksspiels! Der bekannteste Code in unserem System ist der “HIGHROLLER” Code, der
    qualifizierten Spielern außergewöhnliche Vorteile bietet.

    References:
    https://online-spielhallen.de/lapalingo-casino-auszahlung-alles-was-sie-wissen-mussen/

  5. Für alle die noch keine OnlyWin Casino-App installiert haben, gibt es 20 No-Deposit Freispiele zum Slot 777 Fruity Classic von 3Oaks.
    Sowohl der Bonus als auch die Freispielgewinne müssen innerhalb von 30 Tagen mindestens 50-mal umgesetzt werden. Ihr könnt dieses Gratis-Guthaben auf bestimmte Casinospiele oder Wetten verwenden und so
    den Bonus ohne Risiko testen. Wichtig dabei ist, dass die Bonusbedingungen als
    erstes umgesetzt werden.
    Es mag zunächst merkwürdig erscheinen, dass die besten Online Casinos und Spielotheken in Deutschland hohe Bonussummen oder 20
    Freispiele ohne Einzahlung versprechen. Jeder Slot-Bonus in Deutschland ist an bestimmte Bonusbedingungen und AGB geknüpft.
    Die genaue Mindesteinzahlung und der maximale Bonusbetrag
    variieren je nach Anbieter und sind in den Bedingungen des Angebots angegeben.
    Es gibt auch Freispiele, die mit einem anderen Bonus verbunden sind, wie z.B.
    Es kann sein, dass Sie Ihre Freispiele nur für bestimmte Spielautomaten benutzen können. Für Freispiele müssen Sie manchmal, je nach den Bedingungen der Aktion, eine Einzahlung machen. Was einen Freispielbonus von den anderen Boni unterscheidet, ist, dass Sie Ihre Gewinne nicht auszahlen können.

    References:
    https://online-spielhallen.de/rooli-casino-freispiele-dein-tor-zu-spannenden-gewinnen/

  6. For more information, see the developer’s
    privacy policy . Fixed bugs, improved performance, drank way too much coffee.
    You can also track contractual issues, performance, and activities.
    The system will provide you with a unique secrete key or keys to
    participate in the procurement process.
    EGP is designed with innovative measures using information and communication technology to streamline the public procurement with greater efficiency that breaks down the
    physical barriers of space and time to have a transparent procurement process through a wider access to information and
    markets. Other sounds of this letter are in the words father, which developed into another sound, such as in the word ace.
    The letter ‘A’ was in the Phoenician alphabet’s aleph.

    References:
    https://blackcoin.co/excalibur-hotel-casino-in-depth-review/

  7. It can be used to emphasize the essential nature
    of something or someone. So, don’t be afraid to
    explore the thesaurus – your writing will thank you for it!
    Synonyms are powerful tools that can add richness and precision to
    your writing. High-priority
    These show importance because something is required or you cannot skip it.

    Knowing the type helps you pick the best word.

    If you repeat important too often, your writing can feel plain.
    Some show need, some show impact, and some show urgency, so one word may not fit every sentence.
    If it matters because many people respect
    it, choose notable or influential. If it matters because it comes first,
    pick key, main, or primary. If it matters because you need
    it, choose essential, necessary, or vital.

    References:
    https://blackcoin.co/best-online-casino-video-slots-history-and-rules/

  8. You can fine-tune your chatbot in a simple drag-and-drop editor and train it with
    additional data or add custom answers to specific
    questions. Some chatbots utilize Retrieval Augmented Generation (RAG) to overcome the limitations of their pre-programmed responses and access a wider pool of information. Non-AI chatbots use scripted dialog and
    cannot generate any responses that were not pre-programmed into the chatbot.

    For example, an e-commerce company could deploy a chatbot to provide browsing customers
    with more detailed information about the products they’re viewing.

    AI-powered customer service bots are automated systems that use artificial intelligence to manage customer interactions.
    AI bots are designed to handle repetitive tasks while improving the overall customer experience through personalized, context-aware conversations.
    Go to the ChatBot dashboard, select your chatbot, and click on the “Publish” button in the top right corner of your screen.
    Software engineers might want to integrate an AI chatbot directly into their complex product.
    Chatbots can help reduce the number of users requiring human assistance, helping businesses more efficient scale up staff
    to meet increased demand or off-hours requests. Many overseas enterprises offer
    the outsourcing of these functions, but doing so carries its own significant cost and reduces
    control over a brand’s interaction with its customers.

    References:
    https://blackcoin.co/house-of-jack-casino-review/

  9. The mobile site supports Android, iOS, and Windows.
    Even though there is no telephone support, you can always write
    a message via live chat or send them an email. All players have the same chances of winnings
    and rules to follow. Besides, it checks the identity of players.
    This commission is a major regulatory agency that has strict requirements for online casinos to meet.
    The Curacao eGaming Commission checked the ins and outs of this casino to make sure
    it’s safe for Australians to gamble here.

    Customer support is always available via live chat. The menu bar on the left
    side of the page has links to all games, payment methods,
    and competitions. This licensing ensures independent audits, player protection standards,
    and legitimate operational oversight. Evolution Gaming’s
    premium offering includes Lightning Roulette with multiplier features, classic table games, and interactive game shows.

    References:
    https://blackcoin.co/ripper-casino-login-australia-complete-guide/

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>