From daf3efe9ae676fd9c703d98e87678879e47913e7 Mon Sep 17 00:00:00 2001 From: thoughtlessnerd Date: Tue, 26 Sep 2023 21:03:25 +0530 Subject: [PATCH] added NOR gate --- index.html | 2 + scripts/UiFunctions.js | 10 ++++ scripts/logicGatesClasses.js | 101 +++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) diff --git a/index.html b/index.html index f2e2e1c..4fce2bf 100644 --- a/index.html +++ b/index.html @@ -48,6 +48,7 @@
  • Xor node
  • Xand node
  • Nand node
  • +
  • Nor Node
  • \/
  • @@ -76,6 +77,7 @@
  • Xor node
  • Xand node
  • Nand node
  • +
  • Nor Node
  • diff --git a/scripts/UiFunctions.js b/scripts/UiFunctions.js index 0e4867e..6d747e8 100644 --- a/scripts/UiFunctions.js +++ b/scripts/UiFunctions.js @@ -101,6 +101,16 @@ function addNandNode() { selectedNode.selected = true; } +function addNorNode() { + newNode = new NorNode(mouseLine.p, mouseLine.q); + newNodeFixed = false; + hideAddMenu(); + hideSearchBox(); + selectedNode.selected = false; + selectedNode = newNode; + selectedNode.selected = true; +} + function saveFile() { saveString = ""; allNodes.forEach((node) => { diff --git a/scripts/logicGatesClasses.js b/scripts/logicGatesClasses.js index d9dac2e..90f02a5 100644 --- a/scripts/logicGatesClasses.js +++ b/scripts/logicGatesClasses.js @@ -963,3 +963,104 @@ class NandNode { }; } } + +class NorNode { + constructor(x, y) { + this.inputs = []; + this.inputLines = []; + this.outputs = []; + this.outputLines = []; + this.height = basicNodes.height / globalScale; + this.width = basicNodes.width / globalScale; + this.x = x; + this.y = y; + this.fontSize = 15 / globalScale; + let inputGap = this.height / 3; + let outputGap = this.height / 2; + this.type = "MidNode"; + for (let i = 0; i < 2; ++i) { + this.inputs.push(new StartNode(this.x, this.y + inputGap * (i + 1))); + this.inputs[i].group = this; + } + for (let i = 0; i < 1; ++i) { + this.outputs.push( + new EndNode(this.x + this.width, this.y + outputGap * (i + 1)) + ); + this.outputs[i].group = this; + } + this.allNodeIndex = allNodes.length; + this.midNodeIndex = midNodes.length; + allNodes.push(this); + midNodes.push(this); + this.selected = true; + this.draw = () => { + // ctx.fillStyle = "purple"; + ctx.fillStyle = "rgba(92,20,22,0.8)"; + ctx.fillRect(this.x, this.y, this.width, this.height); + ctx.strokeStyle = this.selected ? selectionColor : "#000"; + ctx.strokeRect(this.x, this.y, this.width, this.height); + ctx.fillStyle = "white"; + ctx.textAlign = "center"; + ctx.font = `${this.fontSize}px sans-serif`; + ctx.fillText( + "NOR", + this.x + this.width / 2, + this.y + this.height / 2 + this.fontSize / 4 + ); + for (let i = 0; i < this.inputs.length; ++i) { + this.inputs[i].draw(); + } + for (let i = 0; i < this.outputs.length; ++i) { + this.outputs[i].draw(); + } + this.update(); + }; + this.update = () => { + let res = this.inputs[0].value; + this.outputs[0].value = !res; + if (res) return; + for (let i = 1; i < this.inputs.length; ++i) { + if (this.inputs[i].value) { + res = this.inputs[i].value; + this.outputs[0].value = !res; + return; + } + } + this.outputs[0].value = !res; + // if() this.updateLocation(); + this.saveString = `new NorNode(${this.x}, ${this.y})`; + }; + this.updateLocation = () => { + for (let i = 0; i < this.inputs.length; ++i) { + this.inputs[i].x = this.x; + this.inputs[i].y = this.y + inputGap * (i + 1); + if (this.inputs[i].connectionLine !== null) + this.inputs[i].connectionLine.updateLocation(); + } + for (let i = 0; i < this.outputs.length; ++i) { + this.outputs[i].x = this.x + this.width; + this.outputs[i].y = this.y + outputGap * (i + 1); + if (this.outputs[i].connectionLine !== null) + this.outputs[i].connectionLine.updateLocation(); + } + }; + this.reScale = () => { + this.height = basicNodes.height / globalScale; + this.width = basicNodes.width / globalScale; + this.fontSize = 15 / globalScale; + }; + this.delete = () => { + allNodes[this.allNodeIndex] = undefined; + midNodes[this.midNodeIndex] = undefined; + for (let i = 0; i < this.inputs.length; ++i) { + this.inputs[i].delete(); + } + for (let i = 0; i < this.outputs.length; ++i) { + this.outputs[i].delete(); + } + }; + this.copy = () => { + addNorNode(); + }; + } +}