JSFiddle - React, Tailwind, and code Playground
HTML
<canvas id="main" style="border:1px #eee solid" width="1000" height="1000"></canvas>
<!-- модель -->
<script type="text/objmodel" id="model">v -0.000581696 -0.734665 -0.623267
v 0.000283538 -1 0.286843
v -0.117277 -0.973564 0.306907
v -0.382144 -0.890788 0.221243
v -0.247144 -0.942602 0.276051
v -0.656078 -0.718512 -0.109025
v -0.609847 -0.786562 0.0198068
v -0.66248 -0.632053 -0.244271
v -0.511812 -0.845392 0.127809
v -0.609326 -0.569868 -0.41571
v -0.426949 -0.649767 -0.567143
v -0.201076 -0.720822 -0.633205
v 0.117382 -0.973645 0.306766
v 0.382454 -0.890869 0.221097
v 0.247512 -0.942667 0.275986
v 0.656371 -0.718624 -0.109224
v 0.610273 -0.786562 0.0197893
v 0.66248 -0.631463 -0.244119
v 0.511631 -0.845357 0.127832
v 0.608654 -0.568839 -0.416318
v 0.424663 -0.649937 -0.567418
v 0.198972 -0.720968 -0.633141
v 0.153371 -0.140519 0.477416
v 0.134781 -0.14723 0.48805
v 0.131261 -0.132153 0.49872
v 0.14749 -0.135105 0.489565
v 0.000686924 -0.0534984 0.505694
v 0.075062 -0.0473306 0.49955
v 0.0695841 -0.0997942 0.550277
v 0.0302569 -0.0971868 0.55389
v 0.0745124 -0.135953 0.523215
v 0.0354892 -0.12785 0.535159
v 0.0389268 -0.109557 0.550558
v 0.0734835 -0.11538 0.538029
v 0.106386 -0.125623 0.51513
v 0.107415 -0.144295 0.5047
v 0.15968 -0.130861 0.484794
v 0.156224 -0.123705 0.486735
v 0.15792 -0.107798 0.4677
v 0.163784 -0.126996 0.470144
v 0.154687 -0.134655 0.486373
v 0.160668 -0.135649 0.476189
v 0.143433 -0.119034 0.499854
v 0.131606 -0.0726738 0.476148
v -0.000260156 -0.124553 0.539058
v -0.000646004 -0.110651 0.55444
v 0.100423 -0.10574 0.537643
v 0.126128 -0.115784 0.515598
v 0.250944 -0.275583 0.380381
v 0.221602 -0.335752 0.330174
v 0.275434 -0.301318 0.24751
v 0.307699 -0.239869 0.292356
v 0.332124 -0.0415605 0.412962
v 0.319444 0.00732526 0.451091
v 0.28667 -0.025805 0.481549
v 0.297006 -0.0758717 0.454242
v 0.357953 -0.159928 -0.223225
v 0.303981 -0.144611 -0.343644
v 0.296386 -0.0547553 -0.357792
v 0.357339 -0.0633433 -0.24278
v 0.45086 0.331215...
JavaScript
// CanvasGL and Model class
(function(window, undefined){
var CanvasGL = function(canvas){
this.canvas = canvas;
this.context = canvas.getContext('2d');
}
CanvasGL.prototype = {
set : function(x, y, color){
var data = this.context.createImageData(1, 1);
data.data[0] = color[0];
data.data[1] = color[1];
data.data[2] = color[2];
data.data[3] = 255;
this.context.putImageData(data, x, y);
},
line : function(x1, y1, x2, y2, color){
var steep = false;
if(Math.abs(x1 - x2) < Math.abs(y1 - y2)){
var tmp = x1;
x1 = y1;
y1 = tmp;
tmp = x2;
x2 = y2;
y2 = tmp;
steep = true;
}
if(x1 > x2){
var tmp = x1;
x1 = x2;
x2 = tmp;
tmp = y2;
y2 = y1;
y1 = tmp;
}
var dx = x2 - x1,
dy = y2 - y1,
derror = Math.abs(dy / dx),
error = 0,
y = y1;
for(var x = x1; x <= x2; x++){
if(steep)
this.set(y, x, color);
else
this.set(x, y, color);
error += derror;
if(error > 0.5){
y += (y2 > y1) ? 1 : -1;
error -= 1;
}
}
}
};
var Model = function(content){
this.content = content;
};
Model.prototype = {
verts : function(){
var offset = this.content.indexOf('#'),
count = this.content.substr(offset).split(' ')[1] | 0,
content = this.content.split('#')[0].split('\n'),
array = new Float64Array(count * 3);
for(var i = 0, l = content.length; i < l; i++){
// v -0.000581696 -0.734665 -0.623267
var split = content[i].split(' ');
array[i*3] = split[1];
array[i*3 + 1] = split[2];
array[i*3 + 2] = split[3];
}
return array;
},
faces : function(){
var offset = this.content.lastIndexOf('#'),
count = this.content.substr(offset).split(' ')[1] | 0,
content = this.content.split('f').slice(1).join('f').split('\n'),
array = new Float64Array(count * 3);
for(var l = content.length; l--;){
if(content.pop()[0] == '#') break;
}
for(var i = 0, l = content.length; i < l;...