graviti_model
/
implGUI.js
117 строк · 3.1 Кб
1const EX_PRES = 52const DEBUG = false3const CLEAN = true4
5let scope = {6width: 0,7height: 0,8ctx: null9}
10
11function initGUI () {12let canvas = document.getElementById('view')13if (canvas && canvas.getContext) {14scope.width = window.innerWidth - 415scope.height = window.innerHeight - 416scope.center = {17x: Math.round(scope.width / 2),18y: Math.round(scope.height / 2)19}20
21canvas.width = scope.width22canvas.height = scope.height23
24scope.ctx = canvas.getContext('2d')25
26scope.ctx.fillStyle = 'black'27scope.ctx.fillRect(0,0,scope.width,scope.height)28
29return true30} else {31return false32}33}
34
35function clean (ctx) {36if (CLEAN) {37ctx.fillStyle = 'black'38ctx.fillRect(0,0,scope.width,scope.height)39}40}
41
42function draw (points, view) {43let ctx = scope.ctx44clean(ctx)45
46// let rect = getDrawRect(points)47const POINT_SIZE = 148
49for (let i = 0; i < points.length; i++) {50// ctx.fillStyle = randomColor()51ctx.fillStyle = 'white'52let x = points[i].x - view.x53let y = points[i].y - view.y54
55let kx = scope.width / view.width56let ky = scope.height / view.height57
58let screenX = Math.round(scope.center.x + x * kx)59let screenY = Math.round(scope.center.y - y * ky)60ctx.fillRect(61screenX,62screenY,63POINT_SIZE,64POINT_SIZE65)66printPointInfo(ctx, screenX, screenY, points[i])67}68
69printDataInfo(ctx, points, view)70}
71
72function printPointInfo (ctx, screenX, screenY, point) {73if (DEBUG) {74ctx.fillText(75`${point.m.toExponential(EX_PRES)}`,76screenX + 10, screenY + 1077)78ctx.fillText(79`(${point.x.toExponential(EX_PRES)}, ${point.y.toExponential(EX_PRES)}, ${point.z.toExponential(EX_PRES)})`,80screenX + 10, screenY + 2081)82ctx.fillText(83`(${point.vx.toExponential(EX_PRES)}, ${point.vy.toExponential(EX_PRES)}, ${point.vz.toExponential(EX_PRES)})`,84screenX + 10, screenY + 3085)86}87}
88
89function printDataInfo (ctx, points, view) {90ctx.fillStyle = '#000000'91ctx.fillRect(0, 0, 120, 100)92ctx.fillStyle = '#ffffff'93ctx.fillText(`points: ${points.length.toExponential(EX_PRES)}`, 10,10)94ctx.fillText(`width: ${view.width.toExponential(EX_PRES)}`, 10,20)95ctx.fillText(`height: ${view.height.toExponential(EX_PRES)}`, 10,30)96ctx.fillText(`x: ${view.x.toExponential(EX_PRES)}`, 10,40)97ctx.fillText(`y: ${view.y.toExponential(EX_PRES)}`, 10,50)98}
99
100function randomColor () {101const MIN = 100102const MAX = 255103
104let r = Math.round(MIN + Math.random() * (MAX - MIN))105let g = Math.round(MIN + Math.random() * (MAX - MIN))106let b = Math.round(MIN + Math.random() * (MAX - MIN))107
108r = r.toString(16)109g = g.toString(16)110b = b.toString(16)111
112r = r.length === 1 ? `0${r}` : r113g = g.length === 1 ? `0${g}` : g114b = b.length === 1 ? `0${b}` : b115
116return `#${r}${g}${b}`117}
118