惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
N
Netflix TechBlog - Medium
WordPress大学
WordPress大学
云风的 BLOG
云风的 BLOG
J
Java Code Geeks
V
Visual Studio Blog
H
Help Net Security
Engineering at Meta
Engineering at Meta
Hugging Face - Blog
Hugging Face - Blog
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
博客园 - 【当耐特】
B
Blog
Stack Overflow Blog
Stack Overflow Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
博客园 - 司徒正美
博客园 - 叶小钗
Y
Y Combinator Blog
MyScale Blog
MyScale Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
酷 壳 – CoolShell
酷 壳 – CoolShell

三省吾身丶丶

从一个简单功能的实现,谈谈 react 中的逻辑复用进化过程 在 vue 中使用 jsx 与 class component 的各种姿势 使用 generic-pool 优化 puppeteer 并发问题 再谈中文字体的子集化与动态创建字体 《张乌梅的日记》摘录 一个使用 react 的思想去使用 vue 的方式 React 项目构建 从一个简单的实例看 JavaScript 的异步编程进化历 webpack入坑之旅(零)简介与升级 中文播客推荐 学习 Promise,掌握未来世界 JS 异步编程基础 2017 杭州 nodeParty 记录 一次canvas中文字转化成图片后清晰度丢失的探索 浅谈 electron 中的 session 管理(隔离) FlexBox 布局详解 HTML5常用标签分类 ES6 简单特性学习记录 一张图学习 ES6 中的 React 生命周期与流程 2017,一切才刚刚开始。
从零学习 canvas (一)
guowenfh · 2017-10-24 · via 三省吾身丶丶

由于上一篇描述的原因。有图像处理的需求,于是我就开始学习 canvas 啦,和以前的一样,这一篇也是一边学一边写,敲出来的。有不正确的地方,欢迎指出。
canvas 本身的 api 描述是比较简单,但是衍生出来的东西,操作,图像处理,动画,性能,还是非常的多的。所以对于 canvas 的学习不出意外的话,将会是一个系列。这就是第一篇了。下面就开始吧

在不支持 canvas 的浏览器中,显示标签中的内容。
绘图区域 默认是 300 x 150。
canvas 中的宽高是实际的宽高,css 中的宽高会等比缩放。
在开始绘图之前需要先,获取绘图环境。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
(function() {
document.body.innerHTML='<canvas id="canvas" width="1000" height="1000">浏览器不支持</canvas>'
const canvas = document.querySelector('#canvas');
if (!canvas.getContext) return;
let context = canvas.getContext('2d');
function time() {
context.clearRect(0, 0, canvas.width, canvas.height);
let x = 200;
let y = 200;
let r = 150;

context.lineWidth = 1;
context.beginPath();
Array(60).fill(0).forEach((item, index) => {
context.moveTo(x, y);
context.arc(x,y,r,6 * index * Math.PI / 180,6 * (index + 1) * Math.PI / 180,false);
});
context.closePath();
context.stroke();

context.fillStyle = '#fff';
context.beginPath();
context.moveTo(x, y);
context.arc(x, y, r * 20 / 21, 0, 360 * Math.PI / 180, false);
context.closePath();
context.fill();


context.lineWidth = 2;
context.beginPath();
Array(12).fill(0).forEach((item, index) => {
context.moveTo(x, y);
context.arc(x,y,r,30 * index * Math.PI / 180,30 * (index + 1) * Math.PI / 180,false);
});
context.closePath();
context.stroke();

context.fillStyle = '#fff';
context.beginPath();
context.moveTo(x, y);
context.arc(x, y, r * 19 / 21, 0, 360 * Math.PI / 180, false);
context.closePath();
context.fill();


var date = new Date();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
var hourValue = (-90 + hour * 30 + minute / 2 + second / 60) * Math.PI / 180;
var minuteValue = (-90 + minute * 6 + second / 12) * Math.PI / 180;
var secondValue = (-90 + second * 6) * Math.PI / 180;


context.lineWidth = 4;
context.beginPath();
context.moveTo(x, y);
context.arc(x, y, r * 8 / 21, hourValue, hourValue, false);
context.closePath();
context.stroke();

context.lineWidth = 2;
context.beginPath();
context.moveTo(x, y);
context.arc(x, y, r * 15 / 21, minuteValue, minuteValue, false);
context.closePath();
context.stroke();

context.beginPath();
context.moveTo(x, y);
context.arc(x, y, r * 18 / 21, secondValue, secondValue, false);
context.closePath();
context.stroke();

setTimeout(time, 1000);
}
time();
})();