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,并给需要放大的区域,弄上对应的图像
相关推荐
浪浪山小白兔12 分钟前
CSS 渐变效果详解——线性渐变与径向渐变
前端·css
VillanelleS17 分钟前
React进阶之React状态管理&CRA
前端·javascript·react.js
一路向前的月光21 分钟前
React(5)
前端·react.js·前端框架
LLLuckyGirl~39 分钟前
webpack配置之---output.path
前端·webpack·node.js
前端_yu小白39 分钟前
vue2项目生产环境移除console.log
前端·javascript·vue.js
B.-39 分钟前
Flutter 中的生命周期
android·前端·flutter·ios
吉水于人1 小时前
Arcgis/GeoScene API for JavaScript 三维场景底图网格设为透明
前端·javascript·arcgis
万物得其道者成1 小时前
用户体验UP!响应式网页设计的CSS魔法
前端·css·ux
优雅永不过时·1 小时前
Three.js编辑器百度搜索 Top 1
前端·javascript·学习·编辑器·three
前端Kingtato1 小时前
Node.js中的npm包:从入门到实践指南
前端·npm·node.js