npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pixi-essentials/texture-allocator

v3.0.0

Published

Dynamic texture allocator based on @pixi-essentials/area-allocator

Downloads

3,497

Readme

@pixi-essentials/texture-allocator

This package implements a slab-based texture allocator. It aims to help improve texture batching efficiency by generating atlases on-the-fly. You can allocate and free texture space on demand. The following implementations are provided:

  • TextureAllocator: This generic allocator issues texture that are not backed by any resource. You must handle resource management yourself.
  • AtlasAllocator: This allocator can be used to issue image/canvas/bitmap- backed textures. Each image item is uploaded separately with a texSubImage2D call.
  • CanvasTextureAllocator: This allocator creates texture slabs backed by a canvas. You can draw to that canvas (texture.baseTexture.resource.source.getContext('2d')) to draw each texture.
  • RenderTextureAllocator: This allocator issues render-textures. Since you render directly into the render-texture, it requires no resource management.

Installation :package:

npm install @pixi-essentials/texture-allocator

Usage :page_facing_up:

AtlasAllocator

import { AtlasAllocator } from '@pixi-essentials/texture-allocator';

// Create an atlas allocator
const allocator = new AtlasAllocator();

// Create an image-source
const image = document.createElement('img');

// Load the image
image.src = "example.com/example.jpg";

// After the image loads, create the texture. You must do this *after* the image is loaded so you know the
// width and height needed. Also, the atlas resource requires the image to be loaded; otherwise, it will
// forget to re-upload once it does load.
image.onload = function() {
    // Find the dimensions of the texture. Make sure this is less than 2048x2048, otherwise the allocator
    // will fail to issue a texture.
    const { naturalWidth, naturalHeight } = image;

    // Allocate the texture. Edge padding is included by default.
    const texture = allocator.allocate(naturalWidth, naturalHeight);

    // Use the texture as you'd like. It's yours now!

    // Free the texture once you're done with it!
    allocate.free(texture);
}

CanvasTextureAllocator

import { CanvasTextureAllocator } from '@pixi-essentials/texture-allocator';

import type { BaseImageResource, Texture } from '@pixi/core';

// Create a canvas-texture allocator
const allocator = new CanvasTextureAllocator();

// Allocate a texture
const texture = allocator.allocate(256, 256);

// This will draw our geometry into a texture
function cacheGeometry(texture: Texture) {
    const baseTexture = texture.baseTexture;
    const canvas = (baseTexture.resource as BaseImageResource).source as HTMLCanvasElement;
    const context = canvas.getContext('2d');

    const frame = texture.frame;

    // Our geometry is just a rectangle, nothing silly :P
    context.fillStyle = 'green';
    context.fillRect(frame.left, frame.top, frame.width, frame.height);
}

// Draw stuff into the texture
cacheGeometry(texture);

// Do stuff with the texture, i.e. add a sprite to your scene

RenderTextureAllocator

import { Graphics } from '@pixi/graphics';
import { RenderTextureAllocator } from '@pixi-essentials/texture-allocator';

// Create a render-texture allocator
const allocator = new RenderTextureAllocator();

// Create something complicated. NOTE: This example is not complicated and is not recommended for caching
// into a texture.
const complexGraphics = new Graphics()
    .beginFill(0xff)
    .drawCircle(150, 150, 100, 100)
    .endFill();

// Allocate the texture
const texture = allocator.allocate(0, 0, 250, 250);

// Cache the graphics into this texture
renderer.render(complexGraphics, texture);

// Use the texture instead of the graphics, i.e. add a sprite to your scene