[Canvas 筆記] 建立 canvas (未整理筆記)


Posted by Powerfultraveling 's Blog on 2021-12-07

前言

這篇紀錄在 canvas 上繪圖的第一步,建立 Canvas!

在 HTML 檔案中添加 canvas 標籤

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <canvas></canvas>
    <script src="index.js"></script>
</body>
</html>

在 script 裡設定、建立 canvas

const canvas = document.querySelector("canvas");

//確認瀏覽器有支援 canvas
if(canvas.getContext()){
    // getContext 會回傳一個 object 裡面有各種方法用來繪圖
    const c = canvas.getContext("2d");
    c.width = window.innerWidth;
    c.height = window.innerHeight;
}

這麼一來我們就建立好 canvas 啦 !










Related Posts

Day 32-SMTP & python anywhere

Day 32-SMTP & python anywhere

Stripe Follow Along Dropdown

Stripe Follow Along Dropdown

React(11) - class & function component 比較 & useRef

React(11) - class & function component 比較 & useRef


Comments