In this article we will share with you the deserialization of the 3D pressurer of Canvas in HTML5. In practical applications, I think it is very convenient to be able to operate 3D scene changes by operating JSON files, especially in When the editor drags and drops graphics elements and produces a series of changes on the graphics elements, the data can be reflected to us very intuitively. Here we have simply made a basic example for your reference.
Practice scene reproduction:
First of all, let’s set up the scene of this example. Familiar friends may have already seen it. The scene is divided into three parts: left, upper right, and lower right. HT can easily split the scene through ht.widget.SplitView to achieve a good page layout. Finally, add this split component to the body of the html:
//场景搭建 dataModel = new ht.DataModel();//数据容器 g3d = new ht.graph3d.Graph3dView(dataModel);//3D 组件 propertyView = new ht.widget.PropertyView(dataModel);// 属性组件 formPane = new ht.widget.FormPane();//表单组件 rightSplit = new ht.widget.SplitView(propertyView, formPane, 'v', 100);//分割组件 new ht.widget.SplitView(g3d, rightSplit, 'h', 0.65).addToDOM();
The next step is to add graphics elements to the scene , and add the primitive to the 3D scene. At this time, we can add various attributes, styles and labels to the primitive as marks. The primitive used in this example is a 3D model, and the ht.Default.parseObj function is used to Obj and mtl files are parsed:
//添加模型 var params = {center: true};//JSON格式控制参数 传入 ht.Default.parseObj 函数中 var modelMap = ht.Default.parseObj(meter_obj, meter_mtl, params);//解析obj和mtl文件, 解析后返回的map结构json对象中,每个材质名对应一个模型信息
Of course, the premise is that the two files meter_obj and meter_mtl have been declared. Here we put these two parts into the js file respectively and call them in the header.
We can see from the above animation that the only parts of the model that need to be changed in this example are the "pointer" and the "switch" below, so we obtain these two objs through traversal Part of the model and register the 3D model:
var array = []; for(var name in modelMap){ var model = modelMap[name];//modelMap 中的模型 array.push(model); if(name === 'pointer'){//obj 文件中的一个模型 名称为 pointer model.mat = {//矩阵变化参数,可对模型进行矩阵变化后导入 func: function(data){ var start = Math.PI * 0.736, range = Math.PI * 1.49, angle = start - range * data.a('meter.value') / 4;//动态获取了 meter.value 的值 return ht.Default.createMatrix([//将一组JSON描述的缩放、移动和旋转等操作转换成对应的变化矩阵 { t3: [0, -82.5, 0] }, { r3: [0, 0, angle] }, { t3: [0, 82.5, 0] } ]); } }; } if(name === 'switch'){//obj 文件中的一个模型 名称为 switch model.mat = { func: function(data){ return ht.Default.createMatrix([ { t3: [0, 48.5, 0] }, { r3: [0, 0, data.a('meter.angle')] }, { t3: [0, -48.5, 0] } ]); } }; model.color = { func: function(data){ if(data.a('meter.angle')){ return 'rgb(186, 0, 0)'; }else{ return 'black'; } } }; } } ht.Default.setShape3dModel('meter', array);//注册3D模型,请参考modeling建模手册 第一参数为模型名称,第二参数为 JSON 类型对象
Then the user can directly set the attribute shape3d where needed to use it as the name of the 3D model registered here. We will create 3 nodes below and assign the node Set this 3D model:
for(var i=0; i<p>We add two annotations to the nodes here as text prompts. You can overload getNote/getNote2 (one node in HT supports dual annotations, so note2 is provided as the second Note) The function overloads the naming method of note. Of course, other similar text prompts in HT can also change the display information of the text in this way. Here we obtain the two properties of meter.value and meter.angle through data binding. Dynamic data: </p><pre class="brush:php;toolbar:false">g3d.getNote = function(data){//重载 getNote 方法 return 'Value:' + data.a('meter.value').toFixed(2); }; g3d.getNote2 = function(data){ var value = Math.round(data.a('meter.angle') / Math.PI * 180);//获取了 meter.angle 属性,数据实时变化 return value ? 'Angle:' + value : 'Switch is off'; };
We also used a little care in the display part of the scene~by changing the values of eye and center to achieve the effect of sight from far to near:
var oldEye = g3d.getEye().slice(0), oldCenter = g3d.getCenter().slice(0), newEye = [200, 300, 650], newCenter = [0, params.rawS3[1]/2, 0]; ht.Default.startAnim({//动画 duration: 1000,//持续时间 easing: function(t){ //动画缓动函数,默认采用 ht.Default.animEasing return (t *= 2) <p>The entire left side The implementation is complete ~ Next, it is time to implement the upper right part, the display and control of attribute values. We have added a total of four attributes: name, meter.value, meter.angle and rotation, and change the 3D value by operating the value in the attribute bar through data binding. For the display status and data binding in the model, we get the accessType and the value in name to coordinate the call to this attribute: </p><pre class="brush:php;toolbar:false">propertyView.addProperties([//用 json 的数组参数方式批量添加属性信息 { name: 'name',//属性名 这里不用设置 accessType,因为 accessType 默认的值为 setName/getName 这种格式 editable: true//设置为可编辑状态 }, { name: 'meter.value',//用于存取name属性,该属性结合accessType属性最终实现对Data属性的存取 accessType: 'attr',//通过 getAttr/setAttr 获取或设置属性值 editable: true, slider: { min: 0, max: 4 } }, { name: 'meter.angle', accessType: 'attr', editable: true, formatValue: function(value){//一般用于将数字转换更易读的文本格式 return Math.round(value / Math.PI * 180); }, slider: { min: 0, max: Math.PI, step: Math.PI/180*5,//每移动一下滑动的步进 getToolTip: function(){//设置鼠标放在图元上的文字提示 return Math.round(this.getValue() / Math.PI * 180); } } }, { name: 'rotation', editable: true, formatValue: function(value){ return Math.round(value / Math.PI * 180); }, slider: { min: -Math.PI, max: Math.PI, step: Math.PI/180*5, getToolTip: function(){ return Math.round(this.getValue() / Math.PI * 180); } } } ]);
Finally, we parse the formPane form panel in the lower right part, and formPane adds it to the form through the addRow function. Add rows. There are two rows in this form. The first row has two parts:
formPane.addRow([//向表单组件中添加行 { id: 'export', button: {//按钮 label: 'Export JSON', onClicked: function(){//点击时触发的函数 var json = dataModel.serialize(); formPane.v('textArea', json); } } }, { button: { label: 'Import JSON', onClicked: function(){ dataModel.clear();//清空数据模型 dataModel.deserialize(formPane.v('textArea'));//将获取到的 textArea 中的数据反序列化,是下面一行的 id 值 } } } ], [0.1, 0.1]); //最后的参数是这行的宽度分配比例 小于1的值为比例,大于1为实际值 formPane.addRow([ { id: 'textArea', textArea: { } } ], [0.1], 0.1);
In this way, we can directly see the effect of our modifications in 3D by modifying the property bar or JSON file. ~How about it? Isn’t it cool and fast?
Related recommendations:
JavaScript canvas implements rotation animation
Based on HTML5 Canvas to implement subway station monitoring
html5 Canvas drawing method example of love
The above is the detailed content of 3D pressurer deserialization of Canvas in HTML5. For more information, please follow other related articles on the PHP Chinese website!

The core features of HTML5 include semantic tags, multimedia support, offline storage and local storage, and form enhancement. 1. Semantic tags such as, etc. to improve code readability and SEO effect. 2. Simplify multimedia embedding with labels. 3. Offline storage and local storage such as ApplicationCache and LocalStorage support network-free operation and data storage. 4. Form enhancement introduces new input types and verification properties to simplify processing and verification.

H5 provides a variety of new features and functions, greatly enhancing the capabilities of front-end development. 1. Multimedia support: embed media through and elements, no plug-ins are required. 2. Canvas: Use elements to dynamically render 2D graphics and animations. 3. Local storage: implement persistent data storage through localStorage and sessionStorage to improve user experience.

H5 and HTML5 are different concepts: HTML5 is a version of HTML, containing new elements and APIs; H5 is a mobile application development framework based on HTML5. HTML5 parses and renders code through the browser, while H5 applications need to run containers and interact with native code through JavaScript.

Key elements of HTML5 include,,,,,, etc., which are used to build modern web pages. 1. Define the head content, 2. Used to navigate the link, 3. Represent the content of independent articles, 4. Organize the page content, 5. Display the sidebar content, 6. Define the footer, these elements enhance the structure and functionality of the web page.

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.

HTML5 is the latest version of the Hypertext Markup Language, standardized by W3C. HTML5 introduces new semantic tags, multimedia support and form enhancements, improving web structure, user experience and SEO effects. HTML5 introduces new semantic tags, such as, ,, etc., to make the web page structure clearer and the SEO effect better. HTML5 supports multimedia elements and no third-party plug-ins are required, improving user experience and loading speed. HTML5 enhances form functions and introduces new input types such as, etc., which improves user experience and form verification efficiency.

How to write clean and efficient HTML5 code? The answer is to avoid common mistakes by semanticizing tags, structured code, performance optimization and avoiding common mistakes. 1. Use semantic tags such as, etc. to improve code readability and SEO effect. 2. Keep the code structured and readable, using appropriate indentation and comments. 3. Optimize performance by reducing unnecessary tags, using CDN and compressing code. 4. Avoid common mistakes, such as the tag not closed, and ensure the validity of the code.

H5 improves web user experience with multimedia support, offline storage and performance optimization. 1) Multimedia support: H5 and elements simplify development and improve user experience. 2) Offline storage: WebStorage and IndexedDB allow offline use to improve the experience. 3) Performance optimization: WebWorkers and elements optimize performance to reduce bandwidth consumption.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
