简体   繁体   中英

How can i write text over a canvas animation

So i'm making a online profile for myself, and i was doing research on how to make the landing page as attractive as possible. Came across HTML5 Canvas, did some research and experimentation on making an interactive background, and this is the result (in codepen).

Now, i would like to write my name in the middle of the canvas with the text infront of the animation.

The animation gets called in a recursive loop and the initialization function gets called when the page is resized or refreshed.

Problem is, i can't get the text infront, and for some odd reason, my text shrinks when the page is resized.

Here is my pen

https://codepen.io/hamza-tariq-khan/pen/mzKMNd

<!doctype html5>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Canvas Resize</title>
    <style>
        canvas{
            /* border: 1.5px solid black; */
            /* background-color: red; */
            display: block;
        }

        body{
            margin: 0;
            overflow-x: hidden;
        }

        .wrapper{
            margin : 20px;
        }

    </style>


</head>
<body>
    <canvas></canvas>
    <script src="script.js"></script>

    <div class="wrapper">
        <h1>This is a sample sentence</h1>
    </div>



</body>
</html>

and this is an idea of what i want to achieve. (the writing of Mathew williams infront of the moving background)

http://findmatthew.com

If I were you, I would skip rendering text in the canvas and apply some CSS rules to the div to make it show up on top instead. The CSS approach improves performance and also improves accessibility. A blind person using a screen reader will be able to figure out what's in the div , but reading text in the canvas is not supported. Most importantly, it's a lot easier to reason about HTML and CSS than it is to reason about text rendering in a canvas element. How often will you do that? When you come back to change the code in a month, six months, or a year, it will be easier to change the HTML/CSS than it will be to think through the canvas rendering logic all over again!

To do this, use the following CSS for div wrapper :

.wrapper{
    position: absolute;
    top: 0px;
    color: red;
    font-size: 60px;
    margin : 20px;
}

position: absolute allows the div to overlap the canvas, and properties top , bottom , left and right allow you to control how far the div is offset from the edges of its parent (in this case wrapper is a child of body ). I adjusted color and font-size just to make it obvious that it works.

And here is a link to a working example: https://codepen.io/anon/pen/LqoroR

 var canvas = document.getElementById("dm_graphs"); var ctx = canvas.getContext("2d"); ctx.fillStyle = "blue"; ctx.fillRect(0, 0, canvas.width, canvas.height);
 <style> body, canvas, html { font: 24px sans-serif; height: 100hv; width: 100%; margin: 0; padding: 0; background: #888; color: #135; overflow: hidden } #fs { position: relative } #txt, canvas { position: absolute } #txt { color: yellow; margin: 50px; font: 24px subpixel-antialiased Noto Sans Samaritan; background: transparent } </style>
 <body> <div id="fs"> <canvas id="dm_graphs" width="400" height="300"></canvas> <div id="txt">This is a text over a canvas.</div> </div> </body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM