diff --git a/Perspective.js b/Perspective.js index 2fa510a..3105740 100644 --- a/Perspective.js +++ b/Perspective.js @@ -1,79 +1,35 @@ -function Solid() { +import {Constants} from './defaultSettings.js'; -} +Point.distanceY = function (distance) { + const OD_ = distance + Constants.pd; -Solid.distanceY = function (distance) { - var X = distance + Settings.pd; - var x = distance; - var Y = Settings.canvasHeight - Settings.horizonY; - return Y * x / X; + return Constants.DD_ / OD_ * distance; }; -Solid.distanceX = function (distance, distanceY) { - var y = distanceY; - var Y = Settings.canvasHeight - Settings.horizonY; - var X = distance - Settings.horizonX; - return y / Y * X; -}; +Point.distanceX = function (distance, distanceY) { + // Constants.DD_ / (Constants.DD_ - distanceY) = (distance - Constants.horizonX) / (result - Constants.horizonX) + // (result - Constants.horizonX) = (distance - Constants.horizonX) / Constants.DD_ * (Constants.DD_ - distanceY) + // result = Constants.horizonX + (distance - Constants.horizonX) / Constants.DD_ * (Constants.DD_ - distanceY) -Solid.distanceZ = function (H, distanceY) { - var Y = Settings.canvasHeight - Settings.horizonY; - var y = Y - distanceY; - return y / Y * H; + return Constants.horizonX + (distance - Constants.horizonX) / Constants.DD_ * (Constants.DD_ - distanceY); }; -function Point(x, y, z, options) { - this._x = x || 0; - this._y = y || 0; - this._z = z || 0; - this.options = options || {}; - this.options.color = this.options.color || 'gold'; - - var Y = Point.distanceY(y); - var X = Point.distanceX(x, Y); - - var Z = Point.distanceZ(z, Y); - - this.x = X; - this.y = Y; - this.z = Z; - - this.X = x - X; - this.Y = Settings.canvasHeight - Y; - this.Z = Settings.canvasHeight - Y - Z; -} - -for (var key in Solid) { - if (Solid.hasOwnProperty(key)) Point[key] = Solid[key] -} - -Point.prototype.draw = function () { - ctx.fillStyle = 'gold'; - ctx.beginPath(); - ctx.arc(this.X, this.Z, 3, 0, Math.PI * 2, true); - ctx.fill(); +Point.distanceZ = function (H, distance, distanceX) { + // (distance - Constants.horizonX) / (distanceY - Constants.horizonX) = H / result; + return H / (distance - Constants.horizonX) * (distanceX - Constants.horizonX) }; -Point.prototype.drawProjection = function () { - ctx.strokeStyle = this.options.color; - ctx.beginPath(); - ctx.moveTo(this.X, this.Y); - ctx.lineTo(this.X, this.Z); - ctx.stroke(); -}; +export function Point(x, y, z = 0) { + const Y = Point.distanceY(y); + const X = Point.distanceX(x, Y); -function Segment(A, B, options) { - this._A = A; - this._B = B; + const Z = Point.distanceZ(z,x, X); - this.options = options || {}; - this.options.color = this.options.color || 'gold'; -} + this.x = X; + this.y = Y; + this.z = z; -Segment.prototype.draw = function () { - ctx.strokeStyle = this.options.color; - ctx.beginPath(); - ctx.moveTo(this._A.x, this._B.z); - ctx.lineTo(this._B.x, this._B.z); - ctx.stroke(); -}; \ No newline at end of file + this.X = X; + this.Y = Constants.canvasHeight - Y; + this.Z = Constants.canvasHeight - Y - Z; +} \ No newline at end of file diff --git a/Settings.js b/Settings.js deleted file mode 100644 index 9808fb7..0000000 --- a/Settings.js +++ /dev/null @@ -1,9 +0,0 @@ -var Settings = { - horizonY: 250, - horizonX: 640, - frameCount: 300, - pd: 300, - mostDistanceX: 15, - mostDistanceY: 20, - scale: 5 -}; \ No newline at end of file diff --git a/cubeAngle.jpg b/cubeAngle.jpg new file mode 100644 index 0000000..0440f2a Binary files /dev/null and b/cubeAngle.jpg differ diff --git a/defaultSettings.js b/defaultSettings.js new file mode 100644 index 0000000..60bbe22 --- /dev/null +++ b/defaultSettings.js @@ -0,0 +1,17 @@ +export const Constants = { + "frameCount": 300, + "pd": 800, + "mostDistanceX": 5, + "mostDistanceY": 2, + "scale": 5 +} + +Constants.canvasWidth = window.innerWidth; +Constants.canvasHeight = window.innerHeight; + +Constants.horizonX = 800 || Constants.canvasWidth / 2; +Constants.horizonY = 200 || Constants.canvasHeight / 2; + +Constants.step = Constants.canvasWidth / Constants.scale; + +Constants.DD_ = Constants.canvasHeight - Constants.horizonY; diff --git a/distanceX.jpg b/distanceX.jpg new file mode 100644 index 0000000..58b3960 Binary files /dev/null and b/distanceX.jpg differ diff --git a/distanceXYZ.js b/distanceXYZ.js deleted file mode 100644 index fa9e625..0000000 --- a/distanceXYZ.js +++ /dev/null @@ -1,31 +0,0 @@ -ctx.beginPath(); -ctx.strokeStyle = 'red'; -// -//// Y -// ctx.moveTo(A._y, Settings.canvasHeight); -// ctx.lineTo(Settings.horizonX, Settings.horizonY); -// -//// Y -// ctx.moveTo(0, Settings.canvasHeight); -// ctx.lineTo(Settings.horizonX + Settings.pd, Settings.horizonY); - -//// X -//ctx.moveTo(0, A.Y); -//ctx.lineTo(Settings.canvasWidth, A.Y); -//// X -//ctx.moveTo(Settings.horizonX, Settings.canvasHeight); -//ctx.lineTo(Settings.horizonX, Settings.horizonY); -// -//// X & Z -//ctx.moveTo(A._x, Settings.canvasHeight); -//ctx.lineTo(Settings.horizonX, Settings.horizonY); -// -//// Z -// ctx.moveTo(A._x, canvasHeight - A._z); -// ctx.lineTo(horizonX, horizonY); -// -//// Z -// ctx.moveTo(A._x, canvasHeight); -// ctx.lineTo(A._x, canvasHeight - A._z); - -ctx.stroke(); \ No newline at end of file diff --git a/distanceY.jpg b/distanceY.jpg new file mode 100644 index 0000000..fd95b68 Binary files /dev/null and b/distanceY.jpg differ diff --git a/distanceZ.jpg b/distanceZ.jpg new file mode 100644 index 0000000..f9c4775 Binary files /dev/null and b/distanceZ.jpg differ diff --git a/form.js b/form.js new file mode 100644 index 0000000..fa96097 --- /dev/null +++ b/form.js @@ -0,0 +1,97 @@ +import {Constants} from "./defaultSettings.js"; + +export function addListeners(canvas) { + var form; + + document.addEventListener("DOMContentLoaded", function () { + var body = document.body; + + body.appendChild(canvas); + + form = document.getElementById('settings'); + form.addEventListener('submit', function (event) { + event.preventDefault(); + }); + + var $pd = form.pd; + $pd.value = Constants.pd; + $pd.addEventListener('change', function () { + var value = +this.value; + if (value > 0) { + Constants.pd = value; + } + }); + + var $frameCount = form['frame-count']; + $frameCount.value = Constants.frameCount; + $frameCount.addEventListener('change', function () { + var value = +this.value; + if (value > 0) { + Constants.frameCount = value; + } + }); + + var $distanceX = form['distance-x']; + $distanceX.value = Constants.mostDistanceX; + $distanceX.addEventListener('change', function () { + var value = +this.value; + if (value > 0) { + Constants.mostDistanceX = value; + } + }); + + var $distanceY = form['distance-y']; + $distanceY.value = Constants.mostDistanceY; + $distanceY.addEventListener('change', function () { + var value = +this.value; + if (value >= 0) { + Constants.mostDistanceY = value; + } + }); + + var $scale = form['scale']; + $scale.value = Constants.scale; + $scale.addEventListener('change', function () { + var value = +this.value; + if (value >= 0) { + Constants.scale = value; + Constants.step = Constants.canvasWidth / Constants.scale; + } + }); + + var $horizonX = form['horizon-x']; + $horizonX.value = Constants.horizonX; + $horizonX.addEventListener('change', function () { + Constants.horizonX = +this.value; + }); + + var $horizonY = form['horizon-y']; + $horizonY.value = Constants.horizonY; + $horizonY.addEventListener('change', function () { + Constants.horizonY = +this.value; + }); + + canvas.addEventListener('click', function (event) { + Constants.horizonY = event.layerY; + Constants.horizonX = event.layerX; + + var $horizonX = form['horizon-x']; + $horizonX.value = Constants.horizonX; + + var $horizonY = form['horizon-y']; + $horizonY.value = Constants.horizonY; + + Constants.DD_ = Constants.canvasHeight - Constants.horizonY; + }); + // + // canvas.addEventListener('wheel', function (event) { + // var delta = event.deltaY > 0 ? 10 : -10; + // var cpd = Constants.pd + delta; + // if (cpd > 0) { + // Constants.pd = cpd; + // } + // var $pd = form.pd; + // $pd.value = Constants.pd; + // }); + }); +} \ No newline at end of file diff --git a/index.html b/index.html index 25eba52..93ea319 100644 --- a/index.html +++ b/index.html @@ -4,9 +4,7 @@