首页 > 其他分享 >uniapp+pixijs写的一个转盘

uniapp+pixijs写的一个转盘

时间:2022-11-09 12:00:58浏览次数:44  
标签:uniapp container menu dishName id let pixijs event 转盘

1,turntable.vue文件,这个里面显示菜单的时候,上一行文字还不能消除,我已经在研究了,等我搞好了我会更新这里的。后期等我多研究研究pixijs,我会出一些pixijs的文章。

<template>
    <view>
        <div id='pixi'></div>
    </view>
</template>

<script>
    export default {
        props: ['menu'],
        data() {
            return {
                app: null,
                container: null,
                winW: 0,
            }
        },
        mounted() {
            this.initPixi()
        },

        methods: {

            initPixi() {
                let type = "WebGL"
                if (!PIXI.utils.isWebGLSupported()) {
                    type = "canvas"
                }

                PIXI.utils.sayHello(type)
                this.app = new PIXI.Application({
                    width: 456, // default: 800
                    height: 456, // default: 600
                    antialias: true, // default: false
                    backgroundAlpha: false, // default: false
                    resolution: 1,
                })
                document.getElementById("pixi").appendChild(this.app.view);

                this.container = new PIXI.Container()
                this.app.stage.addChild(this.container)

                let sprite = PIXI.Sprite.from('/static/turnbg3.png');
                this.winW = document.body.clientWidth

                sprite.width = this.winW
                sprite.height = this.winW

                this.container.addChild(sprite);
                this.container.x = this.winW / 2;
                this.container.y = 250;
                this.container.pivot.x = this.container.width / 2
                this.container.pivot.y = this.container.width / 2

                let spritePointer = PIXI.Sprite.from('/static/pointer1.png');

                spritePointer.width = 75
                spritePointer.height = 100
                spritePointer.x = this.winW / 2 - 37
                spritePointer.y = 186

                this.app.stage.addChild(spritePointer);

            },
            rorate() {
                let angle = Math.floor(this.container.angle + Math.random() * 1000)

                let currentAngle = (angle - 45) % 360

                let index = 9 - (Math.floor(currentAngle / 45) == 0 ? 8 : Math.floor(currentAngle / 45))

                let singleAngle = (angle % 360)
                let toatlAngle = angle - (angle % 45) - 45 > 0 ? angle - (angle % 45) - 45 : 0

                // Listen for animate update

                console.log('index', index, angle, currentAngle, this.container.angle, toatlAngle)

                this.app.ticker.add((delta) => {
                    if (this.container.angle < toatlAngle) {
                        this.container.angle = this.container.angle >= toatlAngle ? toatlAngle : (this.container
                            .angle + 3 * delta)
                        // setTimeout(() => {
                        //     let msg = '今日菜单:'
                        //     if (index > this.menu.length) {
                        //         msg += '西北风'
                        //     } else {
                        //         msg += '' + this.menu[index - 1].dishName
                        //     }
                        //     this.currentMenu(msg)
                        // }, 30 * delta)
                    }
                });
                let msg = '今日菜单:'
                if (index > this.menu.length) {
                    msg += '西北风'
                } else {
                    msg += '' + this.menu[index - 1].dishName
                }
                setTimeout(() => {
                    this.currentMenu(msg)
                }, 4000)

            },
            currentMenu(msg) {
                console.log('this.app', this.app.stage.children)
                this.app.stage.children.clear()
                let menuTriangle = new PIXI.Graphics();
                menuTriangle.width = this.winW
                menuTriangle.height = this.winW
                menuTriangle.x = 10
                menuTriangle.y = 0
                let style = new PIXI.TextStyle({
                    fontFamily: 'Arial',
                    fontSize: 12,
                    fill: '#ff5400',
                    lineHeight: 40,
                    // wordWrapWidth: (265 / 3) * 2,
                    wordWrapWidth: this.win - 10,
                    wordWrap: true,
                    breakWords: true,
                });
                let currentMenu = new PIXI.Text(msg, style);
                currentMenu.x = 0 currentMenu.y = 0 menuTriangle
                    .addChild(currentMenu);
                this.app.stage.addChild(menuTriangle);
            }
        }
    }
</script>

<style>
    #id {
        /* text-align: center */
    }
</style>

2,index.vue,界面不够美观,我这个转盘还是花了好久去修改的,因为我本人不会ps,现学的。

<template>
    <view class="content">
        <!-- <image class="logo" src="/static/logo.png"></image> -->
        <view class="button-area">
            <button size='mini' @click='openDialog'>添加菜品</button>
            <button size='mini' @click='rorate'>转动</button>
        </view>

        <view class='menu'>
            <div class='menu-item' v-for='(item,index) in menu' :key='index'>
                <span>
                    <span>{{index +1}}:</span><span class='dish-name'>{{item.dishName}}</span>
                </span>
                <span>
                    <span class='edit-btn' @click='editDialog(item.dishName,item._id)'>编辑</span>
                    <span class='del-btn' @click='delDishName(item._id)'>删除</span>
                </span>
            </div>
        </view>
        <turntable ref='turntable' class='turn-con' :menu='menu'></turntable>
        <view class='pop' v-show='ifVisilble'>
            <form @submit="formSubmit" class='form-con'>
                <view class="input-con">
                    <span>菜单名称:</span>
                    <input class="uni-input" name="input" :value='dishName' placeholder="这是一个输入框" />
                </view>
                <view class="submit-btn">
                    <button size='mini' form-type="submit">提交</button>
                    <button size='mini' form-type="submit" @click='reset'>取消</button>
                </view>
            </form>
        </view>
    </view>
</template>

<script>
    import turntable from '../turntable/turntable.vue'
    export default {
        components: {
            turntable
        },

        data() {
            return {
                title: 'Hello',
                menu: [],
                ifVisilble: false,
                dishName: '',
                isEdit: false
            }
        },
        methods: {
            rorate() {
                this.$refs.turntable.rorate()
            },
            getMenuList(e) {
                uniCloud.callFunction({
                    name: 'menuList',
                    data: {
                        limit: 30,
                        offset: 0,
                        keyWord: e ? e.detail.value : ''
                    }
                }).then((res) => {
                    console.log('菜单', res.result.data)
                    this.menu = res.result.data
                })
            },
            formSubmit(e) {
                console.log('e', e.detail.value.input)
                if (this.isEdit) {
                    uniCloud.callFunction({
                        name: 'updateDishName',
                        data: {
                            id: this.id,
                            dishName: e.detail.value.input
                        }
                    }).then(res => this.getMenuList())
                } else {
                    uniCloud.callFunction({
                        name: 'addDishName',
                        data: {
                            dishName: e.detail.value.input
                        }
                    }).then(res => this.getMenuList())

                }
            },
            delDishName(id) {
                uniCloud.callFunction({
                    name: 'delDishName',
                    data: {
                        id
                    }
                })
                this.getMenuList()
            },
            reset() {
                this.dishName = ''
                this.ifVisilble = false
            },
            openDialog() {
                if (this.menu.length < 8) {
                    this.ifVisilble = true
                    this.dishName = ''
                    this.isEdit = false
                } else {
                    alert('只可以添加八个菜!')
                }

            },
            editDialog(dishName, id) {
                this.ifVisilble = true
                this.isEdit = true
                this.dishName = dishName
                this.id = id
            }
        },
        created() {
            this.getMenuList()
        }
    }
</script>

<style>
    .content {
        /* position: relative; */
    }

    .button-area {
        display: flex;
        justify-content: space-around;
        width: 100%;
        padding-top: 20px;
    }

    .title {
        font-size: 36rpx;
        color: #8f8f94;
    }

    .menu-item {
        display: inline-flex;
        justify-content: space-between;
        width: 50%;
        padding-bottom: 5px;
    }

    .edit-btn {
        padding-right: 5px;
        color: #4054e6b0;
    }

    .del-btn {
        padding-right: 5px;

        color: #e68540;
    }

    .dish-name {
        padding-left: 10px;
        color: #dc40e6
    }

    .menu {
        padding: 20px;
    }

    .input-con {
        display: flex;
        padding: 20px 20px;
    }

    .uni-input {
        border-bottom: 1px solid black;
        padding-left: 20px;
        width: 70%;
    }


    .submit-btn {
        text-align: right;
        padding-right: 20px;
    }

    .submit-btn :first-child {
        margin-right: 10px;
    }

    .turn-con {
        text-align: center;
        padding-top: 0px 20px 0 20px;
    }

    .pop {
        position: relative;
        top: -400px;
        background: #fff;
        padding: 20px 0;
        margin: 0 10px;
        border-radius: 10px;
        box-shadow: 5px 5px 5px #89898773;
    }
</style>

3,云函数文件我就写到一起了,后面大家自己写,可以分开。下面是文函数截图,再下面是里面的内容。有其他配置问题可以评论区问我。

 

 

'use strict'
const db = uniCloud.database()
exports.main = async (event, context) => {
    //event为客户端上传的参数
    console.log('event : ', event, context)
    let data = { dishName: event.dishName }
    await db.collection('menu').add(data)
    //返回数据给客户端
    return {
        code: 200,
        msg: '添加成功'
    }
}

'use strict'
const db = uniCloud.database()
exports.main = async (event, context) => {
    //event为客户端上传的参数
    console.log('event : ', event)
    const id = event.id
    const collection = db.collection('menu')
    const res = collection.doc(id).remove() //返回数据给客户端

    return {
        code: 200,
        msg: '删除成功'
    }
}

'use strict'
const db = uniCloud.database()
exports.main = async (event, context) => {
    //event为客户端上传的参数
    console.log('event : ', event)
    const collection = db.collection('menu') // 获取menu的集合对象
    console.log('collection : ', collection)
    const res = await collection.limit(event.limit).skip(event.offset).where({ dishName: new RegExp(event.keyWord) }).get()
    //返回数据给客户端
    return res
}



'use strict'
const db = uniCloud.database()
exports.main = async (event, context) => {
    //event为客户端上传的参数
    console.log('event : ', event)
    const id = event.id
    const dishName = event.dishName
    const collection = db.collection('menu')
    await collection.doc(id).update({ dishName })
    //返回数据给客户端
    return {
        code: 200,
        msg: '编辑成功'
    }
}

基本代码就是这样啦,细节不明白关注我,然后评论区问我,

标签:uniapp,container,menu,dishName,id,let,pixijs,event,转盘
From: https://www.cnblogs.com/sinceForever/p/16873161.html

相关文章

  • 05 uniapp/微信小程序 项目day05
    ​一.登录与支付1.1登录1.1.1条件判断当我们点击结算应当进行条件判断第一个如果没有勾选商品第二个是没选择地址第三个是未登录1.1.2页面布局应该有两个页......
  • uniapp-uview 微信小程序form中自定义校验规则失效问题。
    uniapp-uview微信小程序form中自定义校验规则失效问题。仔细阅读官方文档,发现有提示。需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则。如......
  • uniapp_01_实现打开文件管理
    *##关于uniapp实现app端文件管理*####前言*####安卓是如何实现的*####uniapp中几种实现方式*####文档里需要用到的主要api介绍*####实现###前言......
  • uniapp计算两组经纬度之间距离
    //计算两组经纬度之间距离constGetDistance=(lat1,lng1,lat2,lng2)=>{ varradLat1=lat1*Math.PI/180.0; varradLat2=lat2*Math.PI/180.0; ......
  • uniapp打包小程序运行到微信开发工具
    1.在manifest.json文件中配置AppID  2.在HBuilderX中配置微信开发者工具的安装路径。工具->设置->运行设置 3.  在微信开发者工具中配置设置->安全设置->开启......
  • uniapp不使用自定义tabbar的情况下隐藏指定的tabbar
    uni给提供的api中uni.hideTabBar()是控制所有的tabbar显示和隐藏,并不能达到我们想要的效果图,但是我仔细看过文档后发现uni.setTabBarItem()中有visible这个选项控......
  • uniapp/微信小程序 项目day03
    一.商品列表1.1获取数据首先能够进入商品列表的途径传的数据有了解了这个之后就可以开始了,先创建分支创建编译模式,并分配初试数据这个时候就可以获取数据了......
  • 处理uniapp激励广告
    使用uniapp查看广告getAdsItem(item){//看广告//console.log(item.adTitle,item)if(app.getIsVisitor()){......
  • uniapp自定义组件的使用--记录
    在pages.json同级目录下创建目录和文件:components:  videoComp:VideoPlaySelf.vueVideoPlaySelf.vue内容<template><videoclass="video"id="myVideo"......
  • uniapp引入微信小程序自定义视频组件--记录
    官方文档:https://zh.uniapp.dcloud.io/tutorial/miniprogram-subject.html在pages.json同级目录下创建目录和文件:wxcomponents:   my-video-play:index.js ......