title:提消返乱示文字
icon:提示的样式,success,none,loading
duration:d出框持续的时间
下面是三种提示框拿档的效果:
先上一图,这个是最终效果:再看看闪耀效果:
1、先将文字画在canvas;
2、利用getImageData()获取图像数据;
3、将图像数据中黑色(你可以用其他颜色)按一定间隔取值(获取坐标);
4、在获取的迅敏核坐标画矩形(你画其他形状也是可以的);
5、使用requestAnimationFrame,变换矩形颜色。
这样就闪烁起来了~~
var i = 0
Page({
/*** 页面的初始数亩掘据*/data: {hideNav: false,colors: ["#fff", "#FF6E40", "#FFAB40", "#FFFF00", "#EEFF41", "#B2FF59", "#69F0AE", "#64FFDA", "#18FFFF", "#40C4FF", "#E040FB", "#FF4081", "#ff5252"],
text: '肖战',scroll: false,setting: false},
/*** 生拿陪命周期函数--监听页面加载*/onLoad: function (options) {let that = this, text = wx.getStorageSync('blinkText') || this.data.textthis.setData({text})this.init()},
init() {wx.createSelectorQuery().select('#canvas').fields({node: true,size: true,}).exec((res) =>{let that = this,text = this.data.textconst width = res[0].widthconst height = res[0].height
const canvas = res[0].nodeconst ctx = canvas.getContext('2d')const dpr = 1 //wx.getSystemInfoSync().pixelRatiocanvas.width = width * dprcanvas.height = height * dprctx.scale(dpr, dpr)i = (canvas.width - that.getByteLen(text) * 100) / 2ctx.fillStyle = "#ffffff"ctx.fillRect(0, 0, canvas.width, canvas.height)ctx.font = "bolder 200px Arial"ctx.fillStyle = 'black'ctx.textBaseline = 'top'ctx.fillText(text, 0, 100)// ctx.lineWidth = 5// ctx.strokeText(text, 0, 100)let imageData = ctx.getImageData(0, 0, canvas.width, canvas.height).data// console.log(imageData)
ctx.fillStyle = "#ffffff"ctx.fillRect(0, 0, canvas.width, canvas.height)
this.data.canvas = canvasthis.data.ctx = ctxthis.data.imageData = imageDataconsole.log(canvas.width, canvas.height)// this.drawText()const renderLoop = () =>{this.drawText()
canvas.requestAnimationFrame(renderLoop)}// canvas.cancelAnimationFrame(renderLoop)canvas.requestAnimationFrame(renderLoop)})},drawText() {var gap = 7,{imageData,canvas,ctx,text,scroll} = this.dataif (scroll) {if (i >= canvas.width) {i = -canvas.width}i += 2}
ctx.clearRect(0, 0, canvas.width, canvas.height)for (var h = 0h <canvas.heighth += gap) {for (var w = 0w <canvas.widthw += gap) {var position = (canvas.width * h + w) * 4var r = imageData[position],g = imageData[position + 1],b = imageData[position + 2]
if (r + g + b == 0) {ctx.fillStyle = this.data.colors[Math.floor(Math.random() * this.data.colors.length)]ctx.fillRect(w + i, h, 5, 5)}}}
},toggleSetting() {this.setData({setting: this.data.setting ? false : true})},setText(e) {
let that = thiswx.cloud.callFunction({name: 'msgSecCheck',data: {op: 'textCheck',content: e.detail.value},success(res) {console.log('ContentCheck-res', res)if (res.result.code == 300) {console.log(res.result.msg)wx.showToast({icon: 'none',title: res.result.msg,})that.setData({'text': ''})} else {that.setData({setting: false,'text': e.detail.value})that.init()}},fail(err) {console.log('ContentCheck-errxxxx', err)
}})
},getByteLen(str) {var len = 0for (var i = 0i <str.lengthi++) {var length = str.charCodeAt(i)if (length >= 0 &&length <= 128) {len += 1} else {len += 2}}console.log('文字长度',len)return len},donothing() {
},/*** 生命周期函数--监听页面初次渲染完成*/onReady: function () {
},
/*** 生命周期函数--监听页面显示*/onShow: function () {
},
/*** 生命周期函数--监听页面隐藏*/onHide: function () {
},
/*** 生命周期函数--监听页面卸载*/onUnload: function () {wx.setStorage({data: this.data.text,key: 'blinkText',})},
/*** 页面相关事件处理函数--监听用户下拉动作*/onPullDownRefresh: function () {
},
/*** 页面上拉触底事件的处理函数*/onReachBottom: function () {
},
/*** 用户点击右上角分享*/onShareAppMessage: function () {
}})
微信小程序生成图片之后,生成图片不清楚。烂贺需要设置canvas里生成图片的参数。destWidth 生成图片逗乎的宽度,数值越大图片越清楚饥指派。
destHeight 生成图片的高度,数值越大图片越清楚。
数值不是越大越好,数值越大,生成的图片越大,图片加载的也就越慢
wx.canvasToTempFilePath({
x: 0,
y: 0,
width: w,
height: h,
destWidth: w,
destHeight: h,
canvasId: 'shareCanvas',
success: function (res) {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
wx.showToast({
title: '图片已经生成',
icon: 'loading',
duration: 1000
})
}
})
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)