Skip to content

Instantly share code, notes, and snippets.

View colinfwren's full-sized avatar

Colin Wren colinfwren

View GitHub Profile
@colinfwren
colinfwren / Page.js
Created September 22, 2021 22:02
Page component to render GraphCMS page
import React from 'react'
import { graphql } from 'gatsby'
import Layout from './Layout'
import SEO from './Seo'
export default function Page({ data: { page }}) {
return (
<Layout>
<SEO data={page.seo}/>
<h1>{page.title}</h1>
@colinfwren
colinfwren / gatsby-node.js
Created September 22, 2021 21:53
Gatsby file to create pages from GraphCMS
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require(`path`)
const MarkdownIt = require('markdown-it')
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
const markdown = new MarkdownIt()
@colinfwren
colinfwren / EditableTextUsage.jsx
Last active March 15, 2022 15:30
A component that uses the editable text component
import React, { useState } from "react";
import { Group, Rect } from "react-konva";
import { EditableText } from "./EditableText";
export function EditableTextUsage({ selected, setSelected }) {
const [isEditing, setIsEditing] = useState(false);
const [isTransforming, setIsTransforming] = useState(false);
const [text, setText] = useState("Click to resize. Double click to edit.");
const [width, setWidth] = useState(200);
const [height, setHeight] = useState(200);
@colinfwren
colinfwren / EditableText.jsx
Created September 10, 2021 22:48
Editable text wrapper component
import React from "react";
import { ResizableText } from "./ResizableText";
import { EditableTextInput } from "./EditableTextInput";
const RETURN_KEY = 13;
const ESCAPE_KEY = 27;
export function EditableText({
x,
y,
@colinfwren
colinfwren / ReizableText.jsx
Last active September 10, 2021 23:25
Reziable Text in Konva
import React, { useRef, useEffect } from "react";
import { Text, Transformer } from "react-konva";
export function ResizableText({
x,
y,
text,
isSelected,
width,
onResize,
@colinfwren
colinfwren / EditableTextInput.jsx
Last active September 10, 2021 22:46
Editable Text component for Konva
import React from "react";
import { Text } from 'react-konva';
import { Html } from "react-konva-utils";
const RETURN_KEY = 13;
const ESCAPE_KEY = 27;
function getStyle(width, height) {
const isFirefox = navigator.userAgent.toLowerCase().indexOf("firefox") > -1;
const baseStyle = {
@colinfwren
colinfwren / updatePathFromMiddle.js
Created August 27, 2021 12:26
Update the path when a middle control point is moved
export function updatePathFromMiddle(path, edge, position) {
const newPoints = path.points.map((item, index) => {
if (edge.points.includes(index)) {
return {
...item,
[edge.axis]: position[edge.axis]
};
}
return item;
});
@colinfwren
colinfwren / updatePathFromExtrude.js
Created August 27, 2021 12:23
Updat the line's path from dragging an extrude control point
export function updatePathFromExtrude(path, edge, position) {
// Create copies so can mutate them
const newPoints = [...path.points];
const newExtrudableEdges = [...path.extrudableEdges];
const newActiveDrag = path.activeDrag;
// Set up configuration, check if starting edge, get the axis to update and the other axis
// with the node to base the other axis' value off
const isStartEdge = edge.points[0] === 0;
const axis = edge.axis;
const otherAxis = edge.axis === "y" ? "x" : "y";
@colinfwren
colinfwren / updatePathFromTip.js
Created August 27, 2021 12:19
Update the line's path when a tip control point is dragged
export function updatePathFromTip(path, index, position) {
// clone points and update position
const newPoints = [...path.points];
newPoints[index] = {
...newPoints[index],
...position
};
// Get the index of the other point to update
const otherIndex = index === 0 ? 1 : index - 1;
// Calculate the axis that the edge leading to the tip is on
@colinfwren
colinfwren / controlPointDragBoundFunc.js
Created August 27, 2021 12:14
Drag Bounds Function for Control Point
function dragBoundFunc(pos, axis, anchorRef) {
const staticPos = anchorRef.current.getAbsolutePosition();
const otherAxis = axis === 'y' ? 'x' : 'y';
return {
[axis]: pos[axis],
[otherAxis]: staticPos[otherAxis]
}
}