Skip to content

Instantly share code, notes, and snippets.

View miltoncandelero's full-sized avatar

Milton Candelero miltoncandelero

View GitHub Profile
@miltoncandelero
miltoncandelero / Resize.ts
Created March 10, 2022 22:27
Snippet to letterbox resize a pixijs canvas
window.addEventListener("resize", ()=>{
const scaleX = window.innerWidth / app.screen.width;
const scaleY = window.innerHeight / app.screen.height;
const scale = Math.min(scaleX, scaleY);
const gameWidth = Math.round(app.screen.width * scale);
const gameHeight = Math.round(app.screen.height * scale);
const marginHorizontal = Math.floor((window.innerWidth - gameWidth) / 2);
const marginVertical = Math.floor((window.innerHeight - gameHeight) / 2);
@miltoncandelero
miltoncandelero / Binary.hx
Last active January 2, 2019 15:12
A binary class for haxe. Oddly anticlimactic how easy it was :|
/**
* Binary class for haxe.
* To make bit masking a bit easier on the eye.
*/
@:forward
abstract Binary(Int) from Int to Int
{
inline public function new(i:Int)
{
this = i;
@miltoncandelero
miltoncandelero / ImageSaver.hx
Last active March 26, 2022 21:11
Save BitmapData as PNG file. (Haxe + OpenFL + HTML5)
//This is not a real .hx class file. Drop this into a class you see fit.
//Feed it a bitmap data and save it to PNG.
public static function saveImage(bitmapData:BitmapData)
{
var b:ByteArray = new ByteArray();
b = bitmapData.encode(bitmapData.rect, new PNGEncoderOptions(true), b);
new FileDialog().save(b, "png", null, "file");
}
@miltoncandelero
miltoncandelero / JsonClass.hx
Last active July 3, 2018 12:48
Creates typedefs based on a json example variable.
import haxe.Json;
/**
* Toss a Dynamic var and get a typedef declaration! (Inspired by http://json2csharp.com)
* (A bit of code stolen from https://gist.github.com/elsassph/16d3b2597f6a51b5817c2fa97dd7f505)
* @author Milton Candelero (Elemental Code)
*/
class JsonClass
{