目录

cocos creator 放大镜效果

  1. 背景图,没啥好说的,就是简单一张图片
  2. 放大镜,就是可以放一张放大镜图片
  3. 蒙层的作用主要就是为了将里面的图片区域弄成圆形,蒙层节点添加mask,配置遮罩层,让里面的图片可以变成圆形
  4. 需要放大的区域就是我们展示放大的区域了
  5. 照相机就是新建一个camera节点了,同时给放大镜添加上一个新的分组,放大镜选择新的分组,然后照相机这里取消勾选新分组,这样屏幕上就不会展示照相机和其子节点了
  • 完成这一步后,预览界面就只会展示一张背景图了,不会展示放大镜
ini 复制代码
const {ccclass, property} = cc._decorator;

@ccclass
export default class MagnifyingMirror extends cc.Component {
    @property(cc.Node)
    mirror: cc.Node = null;
    @property(cc.Node)
    mirrorCameraNode: cc.Node = null;
    @property(cc.Node)
    tempCameraSpriteNode: cc.Node = null;


    viewSize: cc.Size = null;

    onLoad () {
        this.viewSize = cc.view.getVisibleSize();
        this.initCamera();
        // this.mirrorCameraNode.setPosition(this.mirror.getPosition());
  
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStartEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
      }

      touchStartPos: cc.Vec2 = null;
      mirrorOriginPos: cc.Vec2 = null;
      touchStartEvent(event) {
        this.touchStartPos = event.getLocation();
        this.mirrorOriginPos = this.mirror.getPosition();
      }
  
      touchMoveEvent(event) {
        let touchPos: cc.Vec2 = event.getLocation();
        let pos = this.mirrorOriginPos.add(touchPos.subtract(this.touchStartPos))
  
        this.mirror.setPosition(pos);
        this.mirrorCameraNode.setPosition(pos);
      }

      initCamera() {
        let visibleRect = cc.view.getVisibleSize();
        
        let texture = new cc.RenderTexture(); // 创建一个渲染纹理
        texture.initWithSize(visibleRect.width, visibleRect.height); // 设置渲染纹理的大小
        let spriteFrame = new cc.SpriteFrame();
        spriteFrame.setTexture(texture);
        this.mirrorCameraNode.getComponent(cc.Camera).targetTexture = texture; // 设置镜像摄像机的渲染纹理

        this.tempCameraSpriteNode.getComponent(cc.Sprite).spriteFrame = spriteFrame;
        this.tempCameraSpriteNode.scaleY = -4;
        this.tempCameraSpriteNode.scaleX = 4;
      }
}
  • 根结点添加上脚本,对应mirror,mirrorCameraNode,tempCameraSpriteNode,这些选取对应的节点,
kotlin 复制代码
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStartEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
  • touchStartEvent方法就是用来移动放大镜
  • touchMoveEvent方法就是移动放大镜里面的内容
ini 复制代码
      initCamera() {
        let visibleRect = cc.view.getVisibleSize();
        
        let texture = new cc.RenderTexture(); // 创建一个渲染纹理
        texture.initWithSize(visibleRect.width, visibleRect.height); // 设置渲染纹理的大小
        let spriteFrame = new cc.SpriteFrame();
        spriteFrame.setTexture(texture);
        this.mirrorCameraNode.getComponent(cc.Camera).targetTexture = texture; // 设置镜像摄像机的渲染纹理

        this.tempCameraSpriteNode.getComponent(cc.Sprite).spriteFrame = spriteFrame;
        this.tempCameraSpriteNode.scaleY = -4;
        this.tempCameraSpriteNode.scaleX = 4;
      }
  • initCamera方法就是配置新的camera节点,添加上texture,并给需要放大的区域,弄上对应的图像
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
勘察加熊人12 分钟前
fastapi+angular外卖系统
前端·fastapi·angular.js
一 乐16 分钟前
农业电商|基于SprinBoot+vue的农业电商服务系统(源码+数据库+文档)
前端·javascript·数据库·vue.js·农业电商
mysusheng22 分钟前
Chrome 浏览器的很多扩展不能用了
前端·chrome
yeyuningzi24 分钟前
关于软航OFFICE文档控件软件在Chrome 133版本上提示扩展已停用的原因及处理办法
前端·扩展程序·manifest v3·ntko office控件·manifest v2·chrome 扩展机制变化
星空寻流年30 分钟前
css盒子模型第二章(margin padding border content)
前端·css
全宝30 分钟前
❤️前端boy该如何上手HarmonyOS?
前端·harmonyos
fruge32 分钟前
【vue2 + Cesium】相机视角移动+添加模型、模型点击事件
前端
henujolly1 小时前
如何进行技术选型?
前端
束尘1 小时前
React前端开发中实现断点续传
前端·javascript·react.js
一蓑烟雨,一任平生1 小时前
React-state响应式内部数据(类组件&Hook两种方式整理)
前端·javascript·react.js