当前位置:网站首页>利用canvas画图片
利用canvas画图片
2022-07-24 01:46:00 【M78_国产007】
canvas画图片的优点:
一张图片的大小可能有几十M,如果加载在网页中就十分耗费资源;H5时,canvas提供了一个方法 drawImage():将原图片像素的内容复制到画布上,占用资源就会很小,因为画布是由JS代码编写,就算是几千行代码也才可能几K的大小。
drawImage()方法介绍:
他有9个参数,三种使用情况。
9个参数:
第一个参数img是源图片,可以是img元素或Image构造函数创建的屏幕外图片对象;
需要注意的是在上下文对象调用drawImage()时,img图形必须加载完毕,有两种方法可以有效避免img图片未加载就调用,代码说明:
<canvas id="cav" width="600" height="600">
</canvas>
<img src="图片文件路径" id="img1">
<script>
var cav = document.querySelector("#cav")
var ctx = cav.getContext("2d")
var img1=document.querySelector("#img1")
//方法一
img1.onload=function(){
//当img把src的资源加载完毕了这个函数才会运行
ctx.drawImage(img1,100,100)
}
//方法二
window.onload=function(){
//当浏览器的window加载完毕了这个函数才会运行
ctx.drawImage(img1,100,100)
}第二个参数sx(可选) 开始剪切的 x 坐标位置;(图片的左上角为(0,0)点)
第三个参数sy(可选) 开始剪切的 y坐标位置;
第四个参数swidth(可选) 剪切的宽度;
第五个参数swidth(可选) 剪切的高度;
第六个参数dx 在画布上放置图像的 x 坐标位置;
第七个参数dy 在画布上放置图像的y坐标位置;
第八个参数dwidth(可选) 使用的图像的宽度 ;
第九个参数dheight(可选) 使用的图像的高度 ;
三种使用情况:
三个参数时:drawlmage(img, dx, dy)在画布指定位置绘制原图
五个参数时:drawImage(img, dx, dy, dw, dh)在画布指定位置上按原图大小绘制指定大小的图
九个参数时:drawlmage(img, sx, sy, sw, sh, dx, dy, dw, dh)剪切图像,并在画布上定位被剪切的部分。
边栏推荐
- Vessel Segmentation in Retinal Image Based on Retina-GAN
- 暑假第三周
- Structure the second operation of the actual combat battalion module
- Summary of HCIA knowledge points
- Jenkins multitâche construction simultanée
- Kotlin foundation from introduction to advanced series explanation (basic chapter) keyword: suspend
- Express operates mysql. What is wrong with the SQL?
- Arm architecture and programming 2 -- arm architecture (based on Baiwen arm architecture and programming tutorial video)
- 小熊派简介和环境搭建
- 选址与路径规划问题(Lingo,Matlab实现)
猜你喜欢
随机推荐
Vessel Segmentation in Retinal Image Based on Retina-GAN
Rip --- routing information protocol
Hcip experiment
20220723 record an unexplained shutdown of SAP Oracle monitoring service
Exchange 2013 SSL证书安装文档
Arm architecture and programming 6 -- Relocation (based on Baiwen arm architecture and programming tutorial video)
Perlin noise and random terrain
SCM learning notes 9 -- Serial Communication (based on Baiwen STM32F103 series tutorials)
基于强化空间注意力的视网膜网络(ESA-Unet)
"Guanghetong AI intelligent module sca825-w" with full AI performance accelerates the era of e-commerce live broadcast 2.0
SCM learning notes 4--gpio (based on Baiwen STM32F103 series tutorials)
Arm architecture and programming 7 -- exceptions and interrupts (based on Baiwen arm architecture and programming tutorial video)
Express operates mysql. What is wrong with the SQL?
Three document usage
Notes - record a dynamic datasource please check the setting of primary problem solving
小熊派第一天
Spark memory management mechanism new version
Study and use of windows security defect detection tool wesng
C byte array and class mutual conversion
Number of combinations....









