小程序 大轉盤 抽獎 canvas animation

項目需求運用到大轉盤 可設置概率 可直接自定義結果 效果如下?

?

兩種方法及結合法

一 通過canvas 實現 但是因為定時器原因 手機端卡頓嚴重 故而最終使用了方法二 但也是該記錄下 學習canvas

二 通過小程序Api?animation完成完美解決卡頓問題 更精確定位 有樣式缺陷 css無法解決樣式 自動分配問題 故結合一二兩種方法 出現第三種

三?通過小程序Api?animation 做動畫旋轉 樣式用canvas來實現 完美解決各類問題?

話不多說踩坑部分 就不展示 直接上第三種方法

創建動畫區域 (animation) 動畫可參考小程序官方API?animationData動畫參數 在點擊開始的時候填充數據

<view class="wrapper-content" style='margin:0 auto;'>

? <view class="canvas-container">

? ? <view animation="{{animationData}}" class="canvas-content" style='margin:0 auto;'>? ?

? ? </view>

? </view>

</view>

.wrapper-content{

? background: #E0CFBC;

? border-radius: 50%;

? width: 720rpx;

? height: 720rpx;

? padding: 40rpx;

? position: relative;

? box-sizing: border-box;

? z-index: 101;

}

.canvas-container {

? margin: 0 auto;

? position: absolute;

? left: 40rpx;

? top: 40rpx;

? width: 640rpx;

? height: 640rpx;

? border-radius: 50%;

}

.canvas-content {

? overflow: hidden;

? box-sizing: content-box;

? border: 20rpx solid #CDB193;

? position: absolute;

? left: 0;

? top: 0;

? z-index: 1;

? display: block;

? width: 600rpx;

? height: 600rpx;

? border-radius: inherit;

? background-clip: padding-box;

}

定義canvas 畫板區域? 設置ID?

<canvas class="canvas-line" disable-scroll="true" canvas-id="canvas"></canvas>

onReady: function(e) {

? ? let that = this,

? ? ? ? fps = 60,

? ? ? ? awardsConfig = that.data.awardsConfig,

? ? ? ? w = this.data.windowWidth / 750 * 600,

? ? ? ? h = this.data.windowWidth / 750 * 600,

? ? ? ? context = wx.createCanvasContext('canvas'),

? ? ? ? //定義canvas位置

? ? ? ? wheel = new Wheel(w / 2, w / 2, (w / 2) - 0, awardsConfig.slicePrizes),

? ? ? ? animation = new Animation(wheel, { w: w, h: h });

? ? ? ? wheel.prizeWidth = 30;

? ? ? ? wheel.prizeHeight = 30;

? ? // 更新動畫

? ? let update = function () {

? ? ? // 清空

? ? ? context.clearRect(0, 0, w, h);

? ? ? // 畫轉盤

? ? ? wheel.draw(context);

? ? ? // 更新數據

? ? ? animation.draw(context);

? ? ? // 更新數據

? ? ? animation.update();

? ? ? // 繪圖?

? ? ? context.draw()

? ? };

? ? setTimeout(update, 1000 / fps, 1000 / fps)

}

1.awardsConfig.slicePrizes為獎品列表? 因為大轉盤是圓的 所以W H 是一樣的?

2.this.data.windowWidth 是屏幕的寬度 小程序有API 可以獲取? 600是設置的轉盤內容(canvas)的rpx寬度? 用屏幕寬度/750*設置內容寬度rpx = 實際內容寬度的px 值

3.wheel是canvas繪制轉盤的代碼 可自行修改參數

設置點擊抽獎按鈕 兩種方法 一直接在canvas內繪制? 二在HTML里面通過定位實現

提供二 HTML 定位實現按鈕

<view class="canvas-btn" bindtap="getLottery">

? ? ? ? <view>

? ? ? ? ? ? <view style="font-size:56rpx;color:#fff;padding-bottom:4rpx;">開始</view>

? ? ? ? ? ? <view style="font-size:20rpx;color:#6F5942;font-weight:600;">點擊抽獎</view>

? ? ? ? ? </view>

</view>

.canvas-btn {

? display: flex;

? flex-direction: column;

? justify-content: space-around;

? position: absolute;

? left: 50%;

? top: 50%;

? margin-left: -110rpx;

? margin-top: -110rpx;

? z-index: 400;

? width: 220rpx;

? height: 220rpx;

? border-radius: 50%;

? color: #f4e9cc;

? border: 10px solid #cdb193;

? text-align: center;

? font-size: 40rpx;

? text-decoration: none;

? box-sizing: border-box;

}

.canvas-btn>view {

? position: absolute;

? top: 7%;

? left: 7%;

? width: 86%;

? height: 86%;

? border-radius: 50%;

? box-sizing: border-box;

? padding: 22rpx 20rpx 0;

? background: linear-gradient(134deg, rgba(232, 219, 197, 1) 0%, rgba(205, 177, 147, 1) 100%);

? /* box-shadow:0px 5px 7px 0px rgba(255,255,255,1); */

? margin: auto;

? z-index: 102;

}

.canvas-btn::after {

? position: absolute;

? display: block;

? content: ' ';

? left: 39%;

? top: -78%;

? width: 0;

? height: 0;

? overflow: hidden;

? border-width: 80rpx 20rpx 80rpx 20rpx;

? border-style: solid;

? border-color: transparent;

? border-bottom-color: #ffab52;

}

getLottery() {

? ? let that = this;

? ? // 獲取獎品配置

? ? let awardsConfig = that.data.awardsConfig,

? ? ? ? runNum = 12, len = awardsConfig.slicePrizes.length,

? ? ? ? awardIndex = 0;

? ? awardIndex = parseInt(Math.random() * 6)

? ? console.log("獎品序號:" + awardIndex);

? ? // 旋轉抽獎

? ? app.runDegs = app.runDegs || 0

? ? app.runDegs = app.runDegs + (360 - app.runDegs % 360) + (360 * runNum - awardIndex * (360 / len))

? ? //創建動畫

? ? let animationRun = wx.createAnimation({

? ? ? duration: 4000,

? ? ? timingFunction: 'ease'

? ? })

? ? console.log(awardsConfig.slicePrizes[awardIndex]);

? ? that.animationRun = animationRun

? ? animationRun.rotate(app.runDegs - (360 / len * 2 + (360 / len)) ).step()

? ? that.setData({

? ? ? animationData: animationRun.export()

? ? })

? },

點擊按鈕觸發getLottery 進行動畫數據填充 實現旋轉效果??runNum為旋轉的圈數?awardIndex為中獎的序號

整體如此完美實現獻上整體代碼

html

<view class="wrapper-content" style='margin:0 auto;'>

? ? ? ? ? <view class="canvas-container-quiu" wx:for="{{list}}" style="-webkit-transform: rotate({{index * (360/list.length)}}deg);transform: rotate({{index * (360/list.length)}}deg);{{index%2==0?'background:#F2E86D':'background:#ffffff'}}"></view>

? ? ? ? ? <view class="canvas-container">

? ? ? ? ? ? <view animation="{{animationData}}" class="canvas-content" style='margin:0 auto;'>

? ? ? ? ? ? <canvas class="canvas-line" disable-scroll="true" bindtouchstart="canvasTouchStart" bindtouchmove="touchMove" bindtouchend="canvasTouchEnd" canvas-id="canvas"></canvas>

? ? ? ? ? ? </view>

? ? ? ? ? ? <view class="canvas-btn" bindtap="getLottery">

? ? ? ? ? ? ? <view>

? ? ? ? ? ? ? ? <view style="font-size:56rpx;color:#fff;padding-bottom:4rpx;">開始</view>

? ? ? ? ? ? ? ? <view style="font-size:20rpx;color:#6F5942;font-weight:600;">點擊抽獎</view>

? ? ? ? ? ? ? </view>

? ? ? ? ? ? </view>

? ? ? ? ? </view>

? ? ? ? </view>

CSS

.wrapper-content{

? background: #E0CFBC;

? border-radius: 50%;

? width: 720rpx;

? height: 720rpx;

? padding: 40rpx;

? position: relative;

? box-sizing: border-box;

? z-index: 101;

}

.canvas-container ul, .canvas-container li {

? margin: 0;

? padding: 0;

? list-style: none;

}

.canvas-container {

? margin: 0 auto;

? position: absolute;

? left: 40rpx;

? top: 40rpx;

? width: 640rpx;

? height: 640rpx;

? border-radius: 50%;

}

.canvas-container-quiu{

? width: 20rpx;

? height: 20rpx;

? border-radius: 50%;

? position: absolute;

? left:10rpx;

? top: 50%;

? margin-top: -8rpx;

? -webkit-transform-origin: 330rpx 50%;

? transform-origin: 350rpx 50%;

? z-index: 1001;

}

.canvas-content {

? overflow: hidden;

? box-sizing: content-box;

? border: 20rpx solid #CDB193;

? position: absolute;

? left: 0;

? top: 0;

? z-index: 1;

? display: block;

? width: 600rpx;

? height: 600rpx;

? border-radius: inherit;

? background-clip: padding-box;

}

.canvas-btn {

? display: flex;

? flex-direction: column;

? justify-content: space-around;

? position: absolute;

? left: 50%;

? top: 50%;

? margin-left: -110rpx;

? margin-top: -110rpx;

? z-index: 400;

? width: 220rpx;

? height: 220rpx;

? border-radius: 50%;

? color: #f4e9cc;

? border: 10px solid #cdb193;

? text-align: center;

? font-size: 40rpx;

? text-decoration: none;

? box-sizing: border-box;

}

.canvas-btn>view {

? position: absolute;

? top: 7%;

? left: 7%;

? width: 86%;

? height: 86%;

? border-radius: 50%;

? box-sizing: border-box;

? padding: 22rpx 20rpx 0;

? background: linear-gradient(134deg, rgba(232, 219, 197, 1) 0%, rgba(205, 177, 147, 1) 100%);

? /* box-shadow:0px 5px 7px 0px rgba(255,255,255,1); */

? margin: auto;

? z-index: 102;

}

.canvas-btn::after {

? position: absolute;

? display: block;

? content: ' ';

? left: 39%;

? top: -78%;

? width: 0;

? height: 0;

? overflow: hidden;

? border-width: 80rpx 20rpx 80rpx 20rpx;

? border-style: solid;

? border-color: transparent;

? border-bottom-color: #ffab52;

}

js

var app = getApp()

var utils = require('../../utils/utils.js');

var Animation = require('../../utils/Animation.js');

var Wheel = require('../../utils/Wheel.js');

Page({

? /**

? * 頁面的初始數據

? */

? data: {

? ? awardsList: {},

? ? list: [],

? ? statusBarHeight: getApp().globalData.statusBarHeight,

? ? scrollHeight: 200,

? ? windowWidth:0,

? ? windowHeight:0,

? ? awardsConfig: {

? ? ? count: 50,

? ? ? slicePrizes: [

? ? ? ? { text: "恭喜中大獎", img: "/assets/coupon_gold.png", title: "積分券x1", num: "1200", x: "1" },

? ? ? ? { text: "醫療服務費", img: "/assets/coupon_gold.png", title: "積分券x1", num: "50", x: "2" },

? ? ? ? { text: "健康保養費", img: "/assets/coupon_gold.png", title: "積分券x1", num: "500", x: "1" },

? ? ? ? { text: "謝謝參與", img: "/assets/coupon_gold.png", title: "積分券x3", num: "0", x: "2" },

? ? ? ? { text: "青春補償費", img: "/assets/coupon_gold.png", title: "積分券x1", num: "200", x: "1" },

? ? ? ? { text: "感恩奉獻費", img: "/assets/coupon_gold.png", title: "積分券x1", num: "100", x: "2" },

? ? ? ? { text: "咨詢售后費", img: "/assets/coupon_gold.png", title: "積分券x1", num: "150", x: "1" },

? ? ? ? { text: "謝謝參與", img: "/assets/coupon_gold.png", title: "積分券x1", num: "0", x: "1" }, { text: "咨詢售后費", img: "/assets/coupon_gold.png", title: "積分券x1", num: "150", x: "1" },

? ? ? ? { text: "謝謝參與", img: "/assets/coupon_gold.png", title: "積分券x1", num: "0", x: "1" }

? ? ? ],

? ? },

? },

? /**

? * 生命周期函數--監聽頁面加載

? */

? onLoad: function(options) {

? ? var that = this;

? ? var that = this;

? ? wx.getSystemInfo({

? ? ? success: function (res) {

? ? ? ? that.setData({

? ? ? ? ? windowWidth: res.windowWidth,

? ? ? ? ? windowHeight: res.windowHeight,

? ? ? ? ? scrollHeight: res.windowHeight - res.windowWidth / 750 * (getApp().globalData.statusBarHeight * 2 + 98)

? ? ? ? });

? ? ? },

? ? })

? },

? onReady: function(e) {

? ? let that = this,

? ? ? fps = 60, awardsConfig = that.data.awardsConfig,

? ? ? ? w = this.data.windowWidth / 750 * 600,

? ? ? ? h = this.data.windowWidth / 750 * 600,

? ? ? ? context = wx.createCanvasContext('canvas'),

? ? ? ? //定義canvas位置

? ? ? ? wheel = new Wheel(w / 2, w / 2, (w / 2) - 0, awardsConfig.slicePrizes),

? ? ? ? animation = new Animation(wheel, { w: w, h: h });

? ? ? ? wheel.prizeWidth = 30;

? ? ? ? wheel.prizeHeight = 30;

? ? // 更新動畫

? ? let update = function () {

? ? ? // 清空

? ? ? context.clearRect(0, 0, w, h);

? ? ? // 畫轉盤

? ? ? wheel.draw(context);

? ? ? // 更新數據

? ? ? animation.draw(context);

? ? ? // 更新數據

? ? ? animation.update();

? ? ? // 繪圖?

? ? ? context.draw()

? ? };

? ? setTimeout(update, 1000 / fps, 1000 / fps)

? ? wx.getSystemInfo({

? ? ? success: function(res) {

? ? ? ? that.setData({

? ? ? ? ? contentHeight: res.windowHeight

? ? ? ? });

? ? ? },

? ? })

? ? that.setData({

? ? ? count: awardsConfig.count

? ? })

? ? let len = awardsConfig.slicePrizes.length,

? ? ? ? rotateDeg = 360 / len / 2 ,

? ? ? ? list = [],

? ? ? ? turnNum = 1 / len;

? ? for (var i = 0; i < len; i++) {

? ? ? list.push({

? ? ? ? award: awardsConfig.slicePrizes[i].text,

? ? ? });

? ? };

? ? that.setData({

? ? ? list: list.concat(list)

? ? });

? },

? /**

? * 抽獎處理函數:

? */

? getLottery: function () {

? ? let that = this;

? ? // 獲取獎品配置

? ? let awardsConfig = that.data.awardsConfig,

? ? ? ? runNum = 12, len = awardsConfig.slicePrizes.length,

? ? ? ? awardIndex = 0;

? ? awardIndex = parseInt(Math.random() * 6)

? ? console.log("獎品序號:" + awardIndex);

? ? // 旋轉抽獎

? ? app.runDegs = app.runDegs || 0

? ? app.runDegs = app.runDegs + (360 - app.runDegs % 360) + (360 * runNum - awardIndex * (360 / len))

? ? //創建動畫

? ? let animationRun = wx.createAnimation({

? ? ? duration: 4000,

? ? ? timingFunction: 'ease'

? ? })

? ? console.log(awardsConfig.slicePrizes[awardIndex]);

? ? that.animationRun = animationRun

? ? animationRun.rotate(app.runDegs - (360 / len * 2 + (360 / len)) ).step()

? ? that.setData({

? ? ? animationData: animationRun.export()

? ? })

? },

})

?

看完有問題 可以評論? 覺得還可以的話點個贊? 謝謝? 相互交流

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,461評論 6 532
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,538評論 3 417
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,423評論 0 375
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,991評論 1 312
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,761評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,207評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,268評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,419評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,959評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,782評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,983評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,528評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,222評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,653評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,901評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,678評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,978評論 2 374