Browse Source

更新

master
luyisha 2 years ago
parent
commit
b5c8d5df0f
  1. 19
      pages/scratchCard/scratch.js
  2. 28
      pages/scratchCard/scratchCard.vue
  3. 142
      pages/scratchCard/scratchTools.js

19
pages/scratchCard/scratch.js

@ -21,6 +21,7 @@ class Scratch {
this.direction = opts.direction;//号码行数
this.transverse = opts.transverse;//每行个数
this.init()
// console.log(this.scale);
}
init () {
@ -35,8 +36,10 @@ class Scratch {
let sx, sy, swidth, sheight, x, y, width, height;
// 底图是像素1080的宽
sx = (this.screenWidth-this.width)/2*1080/this.screenWidth;
sy = (this.screenWidth/1.5)*1080/this.screenWidth;//开始剪切的 y 坐标位置
// sx = (this.screenWidth-this.width)/2*1080/this.screenWidth;
sx = 65;
// sy = (this.screenWidth/1.5)*1080/this.screenWidth;//开始剪切的 y 坐标位置
sy = 720;//开始剪切的 y 坐标位置
swidth = this.width*1080/this.screenWidth;//被剪切图像的宽度
sheight = this.height*1080/this.screenWidth;//被剪切图像的高度
x = 0;//在画布上放置图像的 x 坐标位置(从画布的左上顶点开始绘制)
@ -48,6 +51,14 @@ class Scratch {
if(this.codeType==='digit_number_icon'){//888
// sy = 820;// 上下刮板一体
sy = 1207;// 上下刮板分体
if(this.canvasId=='guaguaLeft'){
sx = 65;
sy = 927;
}
if(this.canvasId=='guaguaRight'){
sx = 512;
sy = 927;
}
}
if(this.codeType==='digit_icon'&&this.direction==2){//666
sy = 528;
@ -59,7 +70,7 @@ class Scratch {
console.log(sx, sy, swidth, sheight, x, y, width, height);
// 数字计算的稍微会偏0.00几像素
this.ctx.drawImage(this.bgImg, sx, sy, swidth, sheight, x, y, width, height);
// this.ctx.drawImage(this.bgImg, 64.8, 720, 950.4, 864, 0, 0, 330, 300);
// this.ctx.drawImage(this.bgImg, Math.floor(sx), Math.floor(sy), Math.floor(swidth), Math.floor(sheight), x, y, Math.floor(width), Math.floor(height));
this.ctx.setFillStyle(this.maskColor)
this.ctx.fillRect(0, 0, this.width, this.height)
this.ctx.draw()
@ -112,6 +123,8 @@ class Scratch {
}
if (len && this.r * this.r * len > this.scale * this.totalArea) {
this.show = true;
// console.log(this.r * this.r * len);
// console.log(this.scale * this.totalArea);
// console.log('刮奖面积达标');
}
this.clearArcFun(x, y, this.r, this.ctx)

28
pages/scratchCard/scratchCard.vue

@ -1,5 +1,5 @@
<template>
<view class="prizePage" >
<view class="prizePage" :class="{'type8':code==='digit_number_icon', 'type7':(code==='digit_icon' && direction==4), 'type6':(code==='digit_icon' && direction==2)}">
<view class="prizeBg" :style="{'background-image':`url('${ticketInfo.bg_image}')`}"></view>
<view class="prizeContent" :style="{'background-image':`url('${ticketInfo.bg_image}')`}"
:class="{'type8':code==='digit_number_icon', 'type7':(code==='digit_icon' && direction==4), 'type6':(code==='digit_icon' && direction==2)}" >
@ -115,7 +115,7 @@
<script>
import API from '@/common/js/api.js'
import Scratch from './scratch.js'
import ScratchT from './scratchTools.js'
// import ScratchT from './scratchTools.js'
// import VConsole from './renderjs.js'
export default {
data () {
@ -264,8 +264,8 @@ export default {
canvasId: 'guagua',
width: this.scratchWidth,
height: this.scratchHeight,
size: this.scratchSize,
scale: this.scratchScale,
size: (this.code==='digit_icon' && this.direction==2)?6:this.scratchSize,///666
scale: (this.code==='digit_icon' && this.direction==2)?0.95:this.scratchScale,///666
bgImg: this.ticketInfo.bg_image,
screenWidth: this.screenWidth,
codeType: this.code,
@ -273,7 +273,7 @@ export default {
transverse: this.transverse,//
})
if(this.code==='digit_number_icon'){
this.guaCanvasL = new ScratchT(this.$refs.leftCa, {
this.guaCanvasL = new Scratch(this.$refs.leftCa, {
canvasId: 'guaguaLeft',
width: this.scratchTopWidthL,
height: this.scratchTopHeight,
@ -285,7 +285,7 @@ export default {
direction: this.direction,//
transverse: this.transverse,//
})
this.guaCanvasR = new ScratchT(this.$refs.rightCa, {
this.guaCanvasR = new Scratch(this.$refs.rightCa, {
canvasId: 'guaguaRight',
width: this.scratchTopWidthR,
height: this.scratchTopHeight,
@ -408,6 +408,18 @@ export default {
// min-height: calc(100vh - 88rpx - var(--status-bar-height));
height: calc(100vh - 88rpx - var(--status-bar-height));
overflow: hidden;
&.type8{
min-height: calc(100vh - 88rpx - var(--status-bar-height));
height: auto;
}
&.type7{
min-height: calc(100vh - 88rpx - var(--status-bar-height));
height: auto;
}
&.type6{
}
}
.prizeBg{
width: 750rpx;
@ -433,10 +445,12 @@ export default {
height: calc(750rpx * 2163 / 1080);
margin-top: calc((100vh - 88rpx - var(--status-bar-height) - (750rpx * 2163 / 1080)) / 2);
position: relative;
margin-top: 0px;
}
&.type7{
height: calc(750rpx * 2163 / 1080);
margin-top: calc((100vh - 88rpx - var(--status-bar-height) - (750rpx * 2163 / 1080)) / 2);
margin-top: 0px;
}
&.type6{
height: calc(750rpx * 1080 / 1080);
@ -498,7 +512,7 @@ export default {
z-index: 999;
display: none;
width: 88%;
height: 80%;
height: 100%;
border-radius: 50%;
background-color: transparent;
box-sizing: border-box;

142
pages/scratchCard/scratchTools.js

@ -1,142 +0,0 @@
class Scratch {
constructor (page, opts) {
opts = opts || {}
console.log(page, '-------');
this.page = page
this.canvasId = opts.canvasId || 'canvas'
this.width = opts.width || 330
this.height = opts.height || 300
this.screenWidth = opts.screenWidth || 375
// this.maskColor = opts.maskColor || '#D2D2D2'
// this.maskColor = '#dddddd'
this.maskColor = 'transparent';
this.size = opts.size || 10
this.r = this.size * 2
this.area = this.r * this.r
this.scale = opts.scale || 0.3
this.totalArea = this.width * this.height
this.bgImg = opts.bgImg
this.show = false
this.codeType = opts.codeType
this.direction = opts.direction;//号码行数
this.transverse = opts.transverse;//每行个数
this.init()
}
init () {
this.clearPoints = []
this.ctx = uni.createCanvasContext(this.canvasId)
this.drawMask()
this.bindTouch()
}
drawMask () {
let sx, sy, swidth, sheight, x, y, width, height;
// 底图是像素1080的宽
swidth = this.width*1080/this.screenWidth;//被剪切图像的宽度
sheight = this.height*1080/this.screenWidth;//被剪切图像的高度
x = 0;//在画布上放置图像的 x 坐标位置(从画布的左上顶点开始绘制)
y = 0;
width = this.width;//要使用的图像的宽度
height = this.height;
if(this.codeType==='digit_number_icon'){//888
sy = 927;//开始剪切的 y 坐标位置
if(this.canvasId=='guaguaLeft'){
sx = 65;
}
if(this.canvasId=='guaguaRight'){
sx = 512;
}
}
console.log(sx, sy, swidth, sheight, x, y, width, height);
// 数字计算的稍微会偏0.00几像素
this.ctx.drawImage(this.bgImg, sx, sy, swidth, sheight, x, y, width, height);
// this.ctx.drawImage(this.bgImg, 64.8, 720, 950.4, 864, 0, 0, 330, 300);
this.ctx.setFillStyle(this.maskColor)
this.ctx.fillRect(0, 0, this.width, this.height)
this.ctx.draw()
}
bindTouch () {
this.page.touchStart = (e) => {
this.eraser(e, true)
}
this.page.touchMove = (e) => {
this.eraser(e, false)
}
this.page.touchEnd = (e) => {
if (this.show) {
this.ctx.clearRect(0, 0, this.width, this.height)
this.ctx.draw()
// console.log('刮奖面积达标');
}
}
}
eraser (e, bool) {
const len = this.clearPoints.length
let count = 0
const x = e.touches[0].x; const y = e.touches[0].y
const x1 = x - this.size
const y1 = y - this.size
if (bool) {
this.clearPoints.push({
x1: x1,
y1: y1,
x2: x1 + this.r,
y2: y1 + this.r
})
}
for (const item of this.clearPoints) {
if (item.x1 > x || item.y1 > y || item.x2 < x || item.y2 < y) {
count++
} else {
break
}
}
if (len === count) {
this.clearPoints.push({
x1: x1,
y1: y1,
x2: x1 + this.r,
y2: y1 + this.r
})
}
if (len && this.r * this.r * len > this.scale * this.totalArea) {
this.show = true;
// console.log('刮奖面积达标');
}
this.clearArcFun(x, y, this.r, this.ctx)
this.ctx.draw(true);
}
clearArcFun (x, y, r, ctx) {
let stepClear = 1
clearArc(x, y, r)
function clearArc (x, y, radius) {
const calcWidth = radius - stepClear
const calcHeight = Math.sqrt(radius * radius - calcWidth * calcWidth)
const posX = x - calcWidth
const posY = y - calcHeight
const widthX = 2 * calcWidth
const heightY = 2 * calcHeight
if (stepClear <= radius) {
ctx.clearRect(posX, posY, widthX, heightY)
stepClear += 1
clearArc(x, y, radius)
}
}
}
}
export default Scratch
Loading…
Cancel
Save