wx.createSelectorQuery()在components获取Dom节点的使用

本文介绍在小程序中如何利用wx.createSelectorQuery方法获取指定ID的DOM节点,并通过实例演示了如何获得节点的上边界坐标。

1、在components获取Dom节点的使用

const query = wx.createSelectorQuery().in(this)
query.select('#the-id').boundingClientRect(function(res){
      res.top // 这个组件内 #the-id 节点的上边界坐标
    }).exec()
<template> <view class="model-container"> <canvas canvas-id="modelCanvas" type="webgl" id="modelCanvas" :style="{ width: canvasWidth + 'px', height: canvasHeight + 'px' }" ></canvas> </view> </template> <script> import * as THREE from 'threejs-miniprogram'; import { GLTFLoader } from '@/pagesB/three/gltf-loader.js'; export default { props: { modelUrl: String, // GLB模型URL canvasWidth: { type: Number, default: 300 }, canvasHeight: { type: Number, default: 300 } }, data() { return { renderer: null, scene: null, camera: null, model: null, animationId: null }; }, mounted() { this.initScene(); this.loadModel(); }, beforeDestroy() { // 清理资源防止内存泄漏 cancelAnimationFrame(this.animationId); if (this.renderer) this.renderer.dispose(); }, methods: { async initScene() { // 获取Canvas节点 const canvasNode = await new Promise(resolve => { const query = uni.createSelectorQuery().in(this); query.select('#modelCanvas').node(res => resolve(res.node)).exec(); }); // 初始化Three.js场景 this.scene = new THREE.Scene(); this.scene.background = new THREE.Color(0xf0f0f0); // 初始化相机 this.camera = new THREE.PerspectiveCamera( 75, this.canvasWidth / this.canvasHeight, 0.1, 1000 ); this.camera.position.set(0, 0, 5); // 初始化渲染器 this.renderer = new THREE.WebGLRenderer({ canvas: canvasNode, antialias: true, alpha: true }); this.renderer.setSize(this.canvasWidth, this.canvasHeight); this.renderer.setPixelRatio(wx.getSystemInfoSync().pixelRatio); // 添加光源 const ambientLight = new THREE.AmbientLight(0xffffff, 0.6); this.scene.add(ambientLight); const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8); directionalLight.position.set(1, 1, 1); this.scene.add(directionalLight); }, loadModel() { const loader = new GLTFLoader(); // 加载进度回调(可选) loader.manager.onProgress = (url, loaded, total) => { console.log(`加载进度: ${Math.round(loaded/total * 100)}%`); }; loader.load( this.modelUrl, gltf => { this.model = gltf.scene; this.scene.add(this.model); // 居中模型 const box = new THREE.Box3().setFromObject(this.model); const center = box.getCenter(new THREE.Vector3()); this.model.position.sub(center); // 自动旋转模型 this.animateModel(); }, undefined, error => { console.error('GLB模型加载失败:', error); uni.showToast({ title: '模型加载失败', icon: 'none' }); } ); }, animateModel() { const animate = () => { this.animationId = requestAnimationFrame(animate); if (this.model) { this.model.rotation.y += 0.01; // 缓慢旋转模型 } this.renderer.render(this.scene, this.camera); }; animate(); } } } </script> <style scoped> .model-container { display: flex; justify-content: center; align-items: center; background-color: #f5f5f5; border-radius: 12rpx; overflow: hidden; } </style>uniapp开发微信小程序报错,请使用vue2版本修改,并实现组件化
最新发布
09-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值