最近群里有几个人问,如何把echarts的图表贴在three.js的模型上。这个问题其实很简单,因为二者都是渲染成canvas的,直接用echarts生成的canvas当作贴图就可以了。
方法确定可行,那么我们就直接开始撸代码。
先搭建一个three的基本场景起来,这里不在复述。
然后新建一个平面,我们把图片贴在这个平面上即可。
【相关课程推荐:JavaScript视频教程】
addPlane() { var geometry = new THREE.PlaneGeometry(10,10); var material = new THREE.MeshBasicMaterial({ side: THREE.DoubleSide, // transparent:true }); this.plane = new THREE.Mesh(geometry, material); this.scene.add(this.plane); }
摆好相机的角度,此时场景中是一个白板。
然后打开echarts的官网,找到案例,来个仪表盘吧。代码拷贝下来。跑起来。
为了方便演示,我在body中创建了2个div,分别作为three和图表的容器。实际开发中图表的容器不需要显示出来的,也不需要添加到body中的。
<div id="webgl" style="width:512px;height: 512px;float: left;"></div> <div id="echart" style="width:512px;height: 512px;margin-left: 620px;"></div>
var myChart = echarts.init(document.getElementById('echart')); option = { tooltip: { formatter: "{a} <br/>{b} : {c}%" }, //toolbox会在右上角生成两个功能按钮,咱们不需要,直接干掉。 // toolbox: { // feature: { // restore: {}, // saveAsImage: {} // } // }, series: [ { name: '业务指标', type: 'gauge', detail: { formatter: '{value}%' }, data: [{ value: 50, name: '完成率' }] } ] }; option.series[0].data[0].value = (Math.random() * 100).toFixed(2) - 0; myChart.setOption(option, true); const dom = document.getElementById("webgl"); const scene = new Basescene(dom); scene.addPlane();
此时看到下面页面:
方法一:CanvasTexture
three.js有一个api:CanvasTexture。可以传入一个canvas对象,用这个方法可以完成上面的任务。
CanvasTexture( canvas : HTMLElement, mapping : Constant, wrapS : Constant, wrapT : Constant, magFilter : Constant, minFilter : Constant, format : Constant, type : Constant, anisotropy : Number )
changeTextureT(texture){ this.plane.material.map = new THREE.CanvasTexture(texture) this.plane.material.needsUpdate = true var thiscancas = document.getElementById("echart").getElementsByTagName('canvas')[0] scene.changeTextureT(thiscancas) }
运行结果如下,确实不清晰,和他们遇到的问题的一样。尝试把echarts绘制大点,但是这个是自适应的,导致仪表板很丑,不是想象的样子,如果是自己绘制的表格,就可以这样处理了。
方法二:getDataURL
既然echarts也是渲染canvas,看看api,应该有方法导出图片。就是下面的api,而且有可选参数,可以设置分辨率。
changeTextureE(texture){ this.plane.material.map = new THREE.TextureLoader().load(texture) this.plane.material.needsUpdate = true } var texture = myChart.getDataURL({ pixelRatio: 4, backgroundColor: '#fff' }); scene.changeTextureE(texture)
分辨率设置为4确实清晰多了。
下面三个图分别是分辨率1,分辨率4以及方法1绘制的效果对比。
3个图区别很明显,方法2>方法1。该使用什么方法已经很明白了。
下面是动态图片,开始没有贴图,然后贴上方法1生成的贴图,接着闪一下,换成方法2分辨率4生成的贴图。放大还是很清晰的。
最后问题:echarts的图表很多都是有缓冲动画的,如果也想在贴图上实时刷新,可行吗。帧率能跟上吗。
全部代码:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>three.js使用Echarts贴图</title> <script src="../js/three.js"></script> <script src="../js/controls/OrbitControls.js"></script> <script src="./echarts.js"></script> </head> <body> <div id="webgl" style="width:512px;height: 512px;float: left;"></div> <div id="echart" style="width:512px;height: 512px;margin-left: 620px;"></div> <script> var myChart = echarts.init(document.getElementById('echart')); option = { tooltip: { formatter: "{a} <br/>{b} : {c}%" }, // toolbox: { // feature: { // restore: {}, // saveAsImage: {} // } // }, series: [ { name: '业务指标', type: 'gauge', detail: { formatter: '{value}%' }, data: [{ value: 50, name: '完成率' }] } ] }; option.series[0].data[0].value = (Math.random() * 100).toFixed(2) - 0; myChart.setOption(option, true); class Basescene { constructor(dom) { this.id = (new Date()).getTime(); this.dom = dom; this.divWidth = this.dom.offsetWidth; this.divHeight = this.dom.offsetHeight; this.scene = new THREE.Scene(); this.camera = new THREE.PerspectiveCamera(45, this.divWidth / this.divHeight, 1, 2000); this.renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true }); this.controls = new THREE.OrbitControls(this.camera, this.renderer.domElement); this.init(); } init() { this.camera.position.set(0, 0, 20); this.camera.lookAt(this.scene.position); this.renderer.setClearColor(0x222222); this.renderer.setSize(this.divWidth, this.divHeight); this.dom.appendChild(this.renderer.domElement); // this.scene.add(new THREE.AxesHelper(10)); this.animate(); this.addLight(); console.log(this.scene); } addLight() { const light = new THREE.AmbientLight(0xffffff); this.scene.add(light); } render() { this.renderer.render(this.scene, this.camera); } animate = () => { this.request = requestAnimationFrame(this.animate); this.render(); } addPlane() { var geometry = new THREE.PlaneGeometry(10, 10); var material = new THREE.MeshBasicMaterial({ side: THREE.DoubleSide, // transparent:true }); this.plane = new THREE.Mesh(geometry, material); this.scene.add(this.plane); } changeTextureE(texture) { this.plane.material.map = new THREE.TextureLoader().load(texture) this.plane.material.needsUpdate = true } changeTextureT(texture) { this.plane.material.map = new THREE.CanvasTexture(texture) this.plane.material.needsUpdate = true } } const dom = document.getElementById("webgl"); const scene = new Basescene(dom); scene.addPlane(); setTimeout(() => { var thiscancas = document.getElementById("echart").getElementsByTagName('canvas')[0] scene.changeTextureT(thiscancas) }, 2000); setTimeout(() => { var texture = myChart.getDataURL({ pixelRatio: 4, backgroundColor: '#fff' }); scene.changeTextureE(texture) }, 4000); </script> </body> </html>
本文来自 js教程 栏目,欢迎学习!
以上是echarts生成的图表在three.js中的应用详解的详细内容。更多信息请关注PHP中文网其他相关文章!

是的,JavaScript的引擎核心是用C语言编写的。1)C语言提供了高效性能和底层控制,适合JavaScript引擎的开发。2)以V8引擎为例,其核心用C 编写,结合了C的效率和面向对象特性。3)JavaScript引擎的工作原理包括解析、编译和执行,C语言在这些过程中发挥关键作用。

JavaScript是现代网站的核心,因为它增强了网页的交互性和动态性。1)它允许在不刷新页面的情况下改变内容,2)通过DOMAPI操作网页,3)支持复杂的交互效果如动画和拖放,4)优化性能和最佳实践提高用户体验。

C 和JavaScript通过WebAssembly实现互操作性。1)C 代码编译成WebAssembly模块,引入到JavaScript环境中,增强计算能力。2)在游戏开发中,C 处理物理引擎和图形渲染,JavaScript负责游戏逻辑和用户界面。

JavaScript在网站、移动应用、桌面应用和服务器端编程中均有广泛应用。1)在网站开发中,JavaScript与HTML、CSS一起操作DOM,实现动态效果,并支持如jQuery、React等框架。2)通过ReactNative和Ionic,JavaScript用于开发跨平台移动应用。3)Electron框架使JavaScript能构建桌面应用。4)Node.js让JavaScript在服务器端运行,支持高并发请求。

Python更适合数据科学和自动化,JavaScript更适合前端和全栈开发。1.Python在数据科学和机器学习中表现出色,使用NumPy、Pandas等库进行数据处理和建模。2.Python在自动化和脚本编写方面简洁高效。3.JavaScript在前端开发中不可或缺,用于构建动态网页和单页面应用。4.JavaScript通过Node.js在后端开发中发挥作用,支持全栈开发。

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。 1)C 用于解析JavaScript源码并生成抽象语法树。 2)C 负责生成和执行字节码。 3)C 实现JIT编译器,在运行时优化和编译热点代码,显着提高JavaScript的执行效率。

JavaScript在现实世界中的应用包括前端和后端开发。1)通过构建TODO列表应用展示前端应用,涉及DOM操作和事件处理。2)通过Node.js和Express构建RESTfulAPI展示后端应用。

JavaScript在Web开发中的主要用途包括客户端交互、表单验证和异步通信。1)通过DOM操作实现动态内容更新和用户交互;2)在用户提交数据前进行客户端验证,提高用户体验;3)通过AJAX技术实现与服务器的无刷新通信。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 Linux新版
SublimeText3 Linux最新版

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。