Skip to content

Instantly share code, notes, and snippets.

@l0go
Created May 16, 2026 03:23
Show Gist options
  • Select an option

  • Save l0go/d9bf59b8925b88b9147cabc4de0fafa0 to your computer and use it in GitHub Desktop.

Select an option

Save l0go/d9bf59b8925b88b9147cabc4de0fafa0 to your computer and use it in GitHub Desktop.
Flixel GIF Screenshot
package;
import flixel.FlxG;
import flixel.FlxState;
import flixel.text.FlxText;
import haxe.io.BytesOutput;
import format.gif.Writer;
import format.gif.Data.DisposalMethod;
import format.gif.Data.Version;
class PlayState extends FlxState {
override public function create() {
super.create();
this.bgColor = 0xffff0000;
var text = new FlxText();
text.text = "Hello, World!";
text.color = 0xff00ff00;
text.size = 32;
add(text);
haxe.Timer.delay(() -> {
final width = FlxG.stage.window.width;
final height = FlxG.stage.window.height;
final pixels = FlxG.stage.window.readPixels();
var output = new BytesOutput();
var gifWriter = new Writer(output);
var colorMappings = new Map<Int, Int>();
var colorTable = haxe.io.Bytes.alloc(256 * 3);
var i = 0;
for (y in 0...height) {
for (x in 0...width) {
var pixel = pixels.getPixel32(x, y);
if (colorMappings.exists(pixel) == false) {
final r = (pixel >> 24) & 0xff;
final g = (pixel >> 16) & 0xff;
final b = (pixel >> 8) & 0xff;
colorTable.set(i * 3, r);
colorTable.set(i * 3 + 1, g);
colorTable.set(i * 3 + 2, b);
colorMappings[pixel] = i;
i += 1;
}
}
}
var pixelBytes = haxe.io.Bytes.alloc(width * height);
var i = 0;
for (y in 0...height) {
for (x in 0...width) {
var pixel = pixels.getPixel32(x, y);
pixelBytes.set(i, colorMappings[pixel]);
i += 1;
}
}
var frame:format.gif.Data.Frame = {
x: 0,
y: 0,
width: width,
height: height,
localColorTable: true,
interlaced: false,
sorted: false,
localColorTableSize: 256,
pixels: pixelBytes,
colorTable: colorTable,
};
var gif:format.gif.Data = {
version: Version.GIF89a,
logicalScreenDescriptor: {
width: width,
height: height,
sorted: false,
pixelAspectRatio: 1,
hasGlobalColorTable: false,
globalColorTableSize: 0,
colorResolution: 7,
backgroundColorIndex: 0,
},
blocks: new List<format.gif.Data.Block>()
}
gif.blocks.add(Block.BExtension(Extension.EGraphicControl({
disposalMethod: DisposalMethod.NO_ACTION,
userInput: false,
hasTransparentColor: false,
delay: 10,
transparentIndex: 0
})));
gif.blocks.add(Block.BFrame(frame));
gifWriter.write(gif);
sys.io.File.saveBytes("test.gif", output.getBytes());
}, 2000);
}
override public function update(elapsed:Float) {
super.update(elapsed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment