Skip to content

Instantly share code, notes, and snippets.

View redblobgames's full-sized avatar
🥰

Amit Patel redblobgames

🥰
View GitHub Profile
@redblobgames
redblobgames / vue-canvas.js
Last active April 11, 2023 07:31
Vue component for canvas with automatic dependency tracking
/** Canvas component
A generic canvas component that calls a draw function to draw the
contents, and automatically calls it again when anything the draw
function depends on changes. Blog entry:
http://simblob.blogspot.com/2018/03/using-vue-with-canvas.html
Example:
<a-canvas width="500" height="200"
#define M_TAU (2.0*3.1415926535897932384626433832795)
precision mediump float;
uniform float u_t, u_N, u_width, u_radius;
attribute vec2 a_wh;
varying vec2 v_texcoord;
void main () {
float w = a_wh.x; /* which "wedge" we're on */
float h = a_wh.y; /* will be 0 at center of circle and 1 at rim */
@redblobgames
redblobgames / test-picojson-variant-tagged.cpp
Created May 17, 2018 22:06
traverse variant+picojson using tags for variants instead of integer indices
// Copyright 2018 Red Blob Games <redblobgames@gmail.com>
// https://github.com/redblobgames/cpp-traverse
// License: Apache v2.0 <http://www.apache.org/licenses/LICENSE-2.0.html>
#include "traverse.h"
#include "traverse-picojson.h"
#include "traverse-variant.h"
#include "traverse-picojson-variant-tagged.h"
#include "variant-util.h"
@redblobgames
redblobgames / remove-alpha-on-canvas.js
Created September 19, 2018 20:41
Remove alpha channel on a canvas, so it's always transparent or always opaque
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const pixels = imageData.data;
for (let i = 3, n = canvas.width * canvas.height * 4; i < n; i += 4) {
pixels[i] = pixels[i] < 127? 0 : 255
}
ctx.putImageData(imageData, 0, 0);
@redblobgames
redblobgames / README.md
Last active January 2, 2019 17:27 — forked from 1wheel/README.md
blend-alpha

(redblobgames updated this block to use premultiplied alpha - http://apoorvaj.io/alpha-compositing-opengl-blending-and-premultiplied-alpha.html)

I can't figure out why points with gl_FragColor = vec4(255, 255, 255, .1); get darker and then lighter when overlaid with increasing density. (note: it should be vec4(1.0, 1.0, 1.0, 0.1) not 0-255 --redblobgames)

I'm using these blending settings:

blend: {
  enable: true,
 func: {
@redblobgames
redblobgames / INSTALL.CFG
Last active March 20, 2019 15:43
Configuration script in SRE 0.992a (SRE.CFG) vs SRE 0.993a (INSTALL.CFG)
;SRE
;Solar Realms Elite
;0.993a
let GAME := SRE
tell $GAME " " 0.993a " setup:"
if $doorfile == "" goto Local
getcwd CWD
tell "The " $GAME " directory is `" $CWD "'."
@redblobgames
redblobgames / make-instagram-photos.sh
Created June 2, 2019 03:02
Turn a panoramic photo into several square photos for Instagram, so that when you scroll they look like one seamless photo
#!/bin/bash
# As of 2017 Feb, Instagram allows multiple photos to be posted side by side.
# @idealisms posted some panoramas split up into separate photos.
# I wrote this script to generate those square photos from a panorama.
if [ -z "$1" ]; then
echo "Usage: $0 filename.jpg"
exit
fi
@redblobgames
redblobgames / my-modeline.el
Last active April 2, 2021 14:56
amitp's modeline summer 2019
;; You'll need to have the s and powerline packages installed for this modeline to work
(require 's)
(require 'powerline)
(defvar my/mode-line-border 8)
(defvar my/modeline-height 22)
(set-face-attribute 'mode-line nil :family "M+ 1m" :height 120
:background "gray20" :foreground "white"
:weight 'normal
@redblobgames
redblobgames / dark-borders.el
Last active October 15, 2019 11:33
Emacs: dark fringe, line numbers
(set-face-attributes 'header-line nil :inherit 'sample :foreground "gray70" :background "gray20")
(set-face-attributes 'fringe nil :foreground "gray70" :background "gray20")
(cl-loop for buffer in '(" *Echo Area 0*" " *Echo Area 1*") do
(with-current-buffer buffer
(face-remap-add-relative 'default '(:family "Muli" :background "gray20" :foreground "white"))))
(set-face-attributes 'line-number nil :family "M+ 1m" :height 0.6 :foreground "#ccc" :background "gray20")
(set-face-attributes 'line-number-current-line nil :inherit 'line-number :foreground "white" :background "blue")
(when (fboundp 'global-display-line-numbers-mode)
(setq-default display-line-numbers-width 4)
@redblobgames
redblobgames / x-section.xslt
Created July 27, 2019 14:23
Example of how I use xsltproc to expand section headings
<!--
I write my site with xhtml extended with my x: tags, then process it into
regular html using xsltproc. Here's an example of how I use xslt for sections.
1. Expand x: tags to a standard html tag:
<x:section> … </x:section> becomes <section> … </section>
2. Pass attributes down to the html tag:
<x:section class="example"> … </x:section>
becomes <section class="example"> … </section>