image

package module
v0.0.23 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2026 License: MIT Imports: 2 Imported by: 0

README

tinywasm/image

Todo lo relacionado con imágenes para TinyWasm: builders HTML + pipeline de optimización WebP + compresión en cliente.

Tres capas

  1. Builders (github.com/tinywasm/image) — Img, Picture, Source, construcción de elementos HTML. Compila para WASM y backend. Sin etiquetas de construcción (build tags).
  2. Pipeline (github.com/tinywasm/image/min) — Handler, Config, procesamiento WebP. Solo para backend (//go:build !wasm).
  3. Navegador (github.com/tinywasm/image/browser) — Compress, CompressToFit, compresión y redimensionado en el cliente antes de subir. Solo para WASM (//go:build wasm).

El nombre image sombrea el paquete estándar de Go a propósito: el stdlib image es demasiado pesado para TinyGo. Internamente, el pipeline aliasea el stdlib como stdimage.

Compresión en el cliente (Frontend/WASM)

import "github.com/tinywasm/image/browser"

// En el handler del evento 'change' de un <input type="file">:
file := input.Get("files").Index(0)

// Comprimir a 1920px maxEdge, WebP, calidad 0.85
res, err := browser.Compress(file, browser.Config{
    MaxEdge: 1920,
    Quality: 0.85,
    Type:    "image/webp",
})
if err != nil {
    // Manejar error (p.ej. browser.ErrUnsupported)
}

// res.Data contiene los []byte listos para subir por HTTP

Render (Frontend/WASM)

import . "github.com/tinywasm/image"

func (c *Hero) Render() *dom.Element {
    return Img("/img/hero.M.webp", "Hero").Lazy().Size(1024, 512).AsElement()
}

Declaración para procesamiento (image.go, //go:build !wasm)

package herosection
import "github.com/tinywasm/image"

func RenderImages() []image.Asset {
    return []image.Asset{
        {Path: "img/hero.png", Variants: image.AllVariants, Alt: "Hero"},
    }
}

El nombre de archivo debe ser image.go. El pipeline lo detecta automáticamente.

Pipeline (Backend)

import "github.com/tinywasm/image/min"

handler := min.New(&min.Config{RootDir: ".", OutputDir: "web/public/img", Quality: 80})
handler.LoadImages()

La detección de módulos se delega a tinywasm/modfind.

Documentation

Index

Constants

View Source
const AllVariants = VariantS | VariantM | VariantL

AllVariants includes all responsive variants.

Variables

This section is empty.

Functions

func Picture

func Picture() *dom.Element

Picture builds a <picture> element for responsive images.

func Source

func Source(srcset, mediaOrType string) *dom.Element

Source builds a <source> for use inside <picture>. mediaOrType: media query "(max-width: 600px)" or MIME type "image/webp".

Types

type Asset

type Asset struct {
	Path     string  // relative to the module directory: "img/logo.png"
	Variants Variant // e.g., AllVariants, VariantS|VariantM, VariantL
	Alt      string  // SEO alternative text; if empty derived from filename
}

Asset represents an image declaration in a module.

type ImgElement

type ImgElement struct {
	// contains filtered or unexported fields
}

ImgElement wraps *dom.Element to provide a fluent image-specific API.

func Img

func Img(src, alt string) *ImgElement

Img builds an <img> element with src and alt.

func (*ImgElement) AsElement

func (i *ImgElement) AsElement() *dom.Element

AsElement returns the underlying *dom.Element for embedding in Render() trees.

func (*ImgElement) Attr

func (i *ImgElement) Attr(key, val string) *ImgElement

Attr sets an arbitrary attribute.

func (*ImgElement) Class

func (i *ImgElement) Class(classes ...string) *ImgElement

Class adds CSS classes.

func (*ImgElement) Lazy

func (i *ImgElement) Lazy() *ImgElement

Lazy sets loading="lazy".

func (*ImgElement) Size

func (i *ImgElement) Size(w, h int) *ImgElement

Size sets width and height (reduces CLS).

func (*ImgElement) String

func (i *ImgElement) String() string

String serializes the image element (satisfies dom.Component).

type Variant

type Variant uint8

Variant represents a bitmask for responsive image variants.

const (
	VariantS Variant = 1 << iota // 1 — 640px  mobile
	VariantM                     // 2 — 1024px tablet
	VariantL                     // 4 — 1920px desktop
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL