uniapp画布基本使用

摘要:
保存exportdefault{data(){return{gao:1920,kuan:1080,ish5:0}}}},mounted(){//#ifdefH5//监视设备this.ish5的网络状态更改事件

<template>
<view>
<canvas canvas- id="cs"></canvas>
<view @tap="baocun">
保存
</view>
</view>
</template>

<script>
export default {
data() {
return {
gao:1920,
kuan:1080,
ish5:0
}
},
mounted(){
// #ifdef H5
// 监听设备网络状态变化事件
this.ish5=1
// #endif
let self = this;
uni.getSystemInfo({
success: function (res) {
/* console.log(res.model);
console.log(res.pixelRatio);
console.log(res.windowWidth);
console.log(res.windowHeight);
console.log(res.language);
console.log(res.version);
console.log(res.platform);*/
self.gao=res.windowHeight
self.kuan=res.windowWidth
}
});
this.hb()
},
methods: {
baocun(){
var that=this;
uni.canvasToTempFilePath({
x: 0,
y: 0,
that.kuan*0.8,
height: 300,
destWidth: that.kuan*0.8,
destHeight: 300,
canvasId: 'cs',
success: function(res) {
// 在H5平台下,tempFilePath 为 base64
console.log(res.tempFilePath)
if(that.ish5==1){
uni.downloadFile({
url: res.tempFilePath, //仅为示例,并非真实的资源
success: (res) => {
console.log('ss')
console.log(res)
if (res.statusCode === 200) {
console.log('下载成功');
that.saveImg(res.tempFilePath)
}
}
});
}else{
uni.downloadFile({
url: res.tempFilePath, //仅为示例,并非真实的资源
success: (res) => {
if (res.statusCode === 200) {
console.log('下载成功');
}
}
});
}
}
})
},
saveImg: function(url){
var oA = document.createElement("a");
oA.download = '';// 设置下载的文件名,默认是'下载'
oA.href = url;
document.body.appendChild(oA);
oA.click();
oA.remove(); // 下载之后把创建的元素删除
},

hb(){
var that=this;
var ctx = uni.createCanvasContext('cs');
var heng=that.kuan*0.8*0.5-50
console.log(heng)
uni.getImageInfo({
src: "/static/logo.png",
success(res) {
console.log(res.path)
ctx.drawImage(res.path, heng,40, 100, 100)// 设置图片坐标及大小,括号里面的分别是(图片路径,x坐标,y坐标,width,height)
ctx.save();//保存
//ctx.setTextAlign('center')
// ctx.font = 'normal 20px STXingkai'; // 字体
ctx.setFontSize(16) //设置字体大小,默认10
ctx.setFillStyle('#3A85FF') //文字颜色:默认黑色
//ctx.fillText("事事通",that.kuan*0.8*0.5,200);//文字内容、x坐标,y坐标
var str="事事通事事通事事通事事通事事通事事通事事通事事通事事通事事通"
that.drawText(ctx, str, 20, 200, 30,that.kuan*0.8 -40);// 调用行文本换行函数

ctx.draw()//绘制
}
})



// ctx.draw()
},

/**
* 绘制多行文本
* ctx canvas对象
* str 文本
* leftWidth 距离左侧的距离
* initHeight 距离顶部的距离
* titleHeight 文本的高度
* canvasWidth 文本的宽度
* @returns {*}
*/

drawText (ctx, str, leftWidth, initHeight, titleHeight, canvasWidth) {
let lineWidth = 0;
let lastSubStrIndex = 0; //每次开始截取的字符串的索引
for (let i = 0; i < str.length; i++) {
lineWidth += ctx.measureText(str[i]).width;
if (lineWidth > canvasWidth) {
ctx.fillText(str.substring(lastSubStrIndex, i), leftWidth, initHeight); //绘制截取部分
initHeight += 22; //22为 文字大小20 + 2
lineWidth = 0;
lastSubStrIndex = i;
titleHeight += 22;
}
if (i == str.length - 1) { //绘制剩余部分
ctx.fillText(str.substring(lastSubStrIndex, i + 1), leftWidth, initHeight);
}
}
// 标题border-bottom 线距顶部距离
titleHeight = titleHeight + 10;
return titleHeight;
},

}
}
</script>

<style>
#cs{
80%;
height: 300px;
border: 2px solid #3A85FF;
border-radius: 2px;
margin: 0 auto;
margin-top: 100px;
}
.baoc{
80px;
height: 40px;
border-radius: 6px;
line-height: 40px;
text-align: center;
font-size: 14px;
color: white;
margin: 30px auto;
background-color: #3A85FF;
}
</style>

免责声明:文章转载自《uniapp画布基本使用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇机器学习优化器总结h5语音播放(移动端)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

怎么使用vscode合并分支

1.切换分支到本地开发2.代码完成后提交代码到本地仓库3.切换分支到需要merge的test分支先pull一下,之后再合并分支—我开发的是这个feature,就合并这个分支4.当合并分支后,需要重新提交到远程:点击一下,直接提交...

JS学习笔记(一)JS处理JSON数据

在数据传输过程中,json以文本的形式传输,也就是字符串,而JS则对json对象进行操作。因此,JSON对象和JSON字符串之间的相互转换是关键。如果系统提示您找不到toJSONString()和parseJSON()方法,则说明您的json包版本太低。...

C#控件重绘学习(一)

由于需要制作工业控制软件,传统的控制已不能满足实际要求,因此控制的重新绘制迫在眉睫。因为考研花费了很多时间,而C#的学习已经搁浅了很长时间,所以我打算借此机会仔细彻底地研究控件的重新绘制。首先,查看Button的旧背景:namespace:SystemWindows.Forms程序集:System.Windows.FormsButton具有太多属性:例如Ba...

JavaScript算法学习:获取字符串最后一位方法及判断是否以指定字符串开始或结尾

Str.substr,其中start是必需的参数,表示坐标的起始位置。正值在正方向计数,负值在反方向计数,长度是可选参数,表示从起始位置开始计数的数字。...

flutter vscode+第三方安卓模拟器

1.首先打开夜曲模拟器2.Win+R,选择cmd,在第三方模拟器安装目录的bin目录下输入夜曲模拟器,然后运行命令:nox_Adb.execonnect127.0.0.1:620013。打开项目终端的vscode并建立连接:adbconnect127.00.1:62001(夜神模拟器的默认端口)4。查看连接:adbdevices或不使用第三方模拟器:1.打开...

rz上传文件及出错解决方案

原始链接:https://blog.csdn.net/yjk13703623757/article/details/87083997单独使用rz命令时有两个问题:上载中断和文件更改。解决方案是使用rz be进行上传,并在弹出对话框中删除“UploadfilesasASCII”之前的复选框。如果使用不带参数的rz命令上传一个大文件,则在上传过程中通常会中断。很...