Skip to content

Instantly share code, notes, and snippets.

View technotariat's full-sized avatar

Colin technotariat

  • Leeds, UK
View GitHub Profile
@technotariat
technotariat / calculateEdgeMiddlePos.js
Created August 27, 2021 12:09
Calculate the middle position of an edge of the path
export function getControlPointCoords(points, edge) {
const affectedPoints = edge.points.map((i) => points[i]);
const otherAxis = edge.axis === 'y' ? 'x' : 'y';
return {
[otherAxis]: affectedPoints[0][otherAxis] + (affectedPoints[1][otherAxis] - affectedPoints[0][otherAxis]) / 2,
[edge.axis]: affectedPoints[0][edge.axis],
}
}
@technotariat
technotariat / calculateEdges.js
Last active August 27, 2021 12:03
Calculating edges along a line
export function getControlPointAxis(point1, point2) {
if (point1.y === point2.y && point1.x !== point2.x) {
return "y";
}
return "x";
}
function calculateExtrudableEdges(points) {
const endPoints = [points.length - 2, points.length - 1];
return [
@technotariat
technotariat / CreatingConnectionOnRelease.js
Last active August 12, 2021 16:27
Creates a connection between objects on the canvas
import React, { useState } from "react";
import { render } from "react-dom";
import { Stage, Layer, Rect, Text, Line } from "react-konva";
import { INITIAL_STATE, SIZE } from "./config";
import { Border } from "./Border";
function createConnectionPoints(source, destination) {
return [source.x, source.y, destination.x, destination.y];
}
@technotariat
technotariat / connectionPreview.js
Last active August 12, 2021 16:28
Functions for showing a connection preview when drag happens
import React, { useState } from "react";
import { render } from "react-dom";
import { Stage, Layer, Rect, Text, Line } from "react-konva";
import { INITIAL_STATE, SIZE } from "./config";
import { Border } from "./Border";
function createConnectionPoints(source, destination) {
return [source.x, source.y, destination.x, destination.y];
}
@technotariat
technotariat / Border.jsx
Created August 12, 2021 16:14
A border component with anchor points
import React from "react";
import { Line, Circle } from "react-konva";
const SIZE = 50
const points = [0, 0, SIZE, 0, SIZE, SIZE, 0, SIZE, 0, 0];
function getAnchorPoints(x, y) {
const halfSize = SIZE / 2;
return [
{
@technotariat
technotariat / SelectionBorder.js
Last active August 12, 2021 17:29
Code for showing a connecition border over an item on screen
import React from "react";
import { Stage, Layer, Rect, Text, Line } from "react-konva";
const SIZE = 50;
const points = [0, 0, SIZE, 0, SIZE, SIZE, 0, SIZE, 0, 0];
function Border({ step, id }) {
const { x, y } = step;
return (
<Line
@technotariat
technotariat / connectionDataStructure.json
Created August 12, 2021 16:05
Data structure used to model a connection between two objects
{
"to": "object-1",
"from": "object-2",
"offsets": {
"to": {
"x": -15,
"y": 305
},
"from": {
"x": 170,
function addItem(e) {
e.preventDefault();
const stage = stageRef.current;
setItems([
...items,
{
x: stage.width() / 2,
y: stage.height() / 2,
type: 'circle'
}
import React, { useState, useRef } from "react";
import { Stage, Layer, Circle, Rect } from "react-konva";
function getItem(item) {
if (item.type === "circle") {
return <Circle x={item.x} y={item.y} fill="red" radius={20} />;
}
return <Rect x={item.x} y={item.y} fill="green" width={20} height={20} />;
}
.card.hide {
opacity: 0;
}