当前位置:网站首页>Canvas - drawing pictures - dynamic drawing production
Canvas - drawing pictures - dynamic drawing production
2022-07-26 03:10:00 【ca11meback】
picture :
drawImage(): Copy the contents of the original picture pixels to the canvas ;
The first parameter is the source image , It can be img Element or Image Constructor to create an off screen picture object ;
When there are three parameters : Specify the drawing of the picture x、y coordinate ;
Five parameters : Specify the drawing of the picture x、y coordinate , And the width of the picture 、 Height ;
Nine parameters : Cropped objects The position of the crop (x,y); The width and height of the crop (w,h); The position of the picture after clipping (x,y); The width and height shown in the picture (w,h);
The first parameter is the source image , It can be img Element or Image Constructor to create an off screen picture object ;
That means you can't put the address of the picture , Otherwise, it cannot be drawn , We need to load the pictures and then canvas Draw in Sketchpad
eg:ctx.drawImage("./src/h5.png")( Such code is wrong )
eg:
var img1=new Image()
img1.src="./src/h5.png"
img1.onload=function(){
ctx.drawImage(img1,100,100)
}This is the right way to write ( recommend )
Of course, there is another way to write it directly in body It says in it img label , And then again js Code to get , But it will show in the page , And it also needs to adopt onload The function says (), because js Code must be in img Only after the tag is loaded can we get
The making of moving picture :
Code thinking :
Load pictures in the timer ( Sprite chart ), Then crop the image in different positions , Displayed in the canvas On the canvas
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<style>
#box {
border: 1px black solid;
}
</style>
<body>
<!-- <img src="./img/h.png" alt=""> -->
<canvas id="box" height="700" width="700"> </canvas>
<script>
/** @type {HTMLCanvasElement} */
var canvas = document.querySelector("#box")
var ctx = canvas.getContext("2d")
var img1=new Image()
img1.src="./img/h.png"
img1.onload=function(){
var n=0
setInterval(()=>{
canvas.width=canvas.width
n++,
n=n%10,
ctx.drawImage(img1,0+n*108,0,108,101,300,300,108,101)
},200)
}
</script>
</body>
</html>n=n%10 To ensure the n Value range of , So that the range of cropping will not exceed the size of the image , Then the canvas will be emptied before each timer call
边栏推荐
- Unknown-Aware Object Detection:Learning What You Don’t Know from Videos in the Wild(CVPR 2022)
- The difference between the world wide web, the Internet and the Internet
- Summary of Huawei virtualization fusioncompute knowledge points
- .net serialize enumeration as string
- 朋友刚学完自动化测试就拿25Koffer,我功能测试何时才能到头?
- Jsd-2204-cool shark Mall (Management Commodity module) -day02
- [C Advanced] deeply explore the storage of data (in-depth analysis + interpretation of typical examples)
- Anti electronic ink screen st7302
- Installation and operation of orb-slam2 under ROS
- C language layered understanding (C language function)
猜你喜欢

中国信通院陈屹力:降本增效是企业云原生应用的最大价值

Managing databases in a hybrid cloud: eight key considerations

Arthas view the source code of the loaded class (JAD)

STM32——PWM学习笔记

STM32——DMA笔记

Difference between soft link and hard link

这种动态规划你见过吗——状态机动态规划之股票问题(上)

【C进阶】深入探索数据的存储(深度剖析+典例解读)

Swin Transformer【Backbone】
![[translation] safety. Value of sboms](/img/8b/1ad825e7c9b6a87ca1fea812556f3a.jpg)
[translation] safety. Value of sboms
随机推荐
STM - exti external interrupt learning notes
Pit trodden when copying list: shallow copy and deep copy
STM32 - DMA notes
STM32——串口学习笔记(一个字节、16位数据、字符串、数组)
GoLang 抽奖系统 设计
多线程编程
LeetCode·每日一题·剑指 Offer || 115.重建序列·拓扑排序
snownlp库各功能及用法
实现一个方法,找出数组中的第k大和第m大的数字相加之和
Autojs cloud control source code + display
【尤里复裂人】带你轻松理解——深拷贝和浅拷贝
STM——EXTI外部中断学习笔记
FPGA_ Initial use process of vivado software_ Ultra detailed
持续交付和DevOps是一对好基友
堆内存与栈内存的区别?
Opening method of win11 microphone permission
[NOIP2001 普及组] 最大公约数和最小公倍数问题
Chen Yili, China Academy of communications technology: cost reduction and efficiency increase are the greatest value of Enterprise Cloud native applications
Jsd-2204-cool shark Mall (Management Commodity module) -day02
Small test (I)