NaraMotion / examples /05_numpy_vector_overlay.py
ArifiIslam's picture
Release NaraMotion v0.26.0: Structure of Arrays (SoA), Path Arena Allocator, Uniform Grid Indexing, Dual-Buffer Rendering & Centroid Spring Pop-in
b516e00 verified
Raw History Blame Contribute Delete
2.44 kB
"""
NaraMotion Example 05: High Performance NumPy Interoperability & Vector HUD
"""
import math
import os
import numpy as np
from naramotion import Image, Context, Format, Font, Circle, Rect
def get_font():
for p in ["C:\\Windows\\Fonts\\segoeui.ttf", "C:\\Windows\\Fonts\\arial.ttf"]:
if os.path.exists(p):
return p
return None
def main():
width, height = 800, 600
img = Image(width, height, Format.PRGB32)
# 1. Zero-Copy NumPy background generation (Plasma / Wave field)
# Blend2D memory buffer is accessed directly without memory copies!
buf = img.to_numpy(copy=False)
y_coords, x_coords = np.mgrid[0:height, 0:width]
cx, cy = width / 2.0, height / 2.0
r = np.sqrt((x_coords - cx) ** 2 + (y_coords - cy) ** 2)
# Calculate plasma wave interference pattern
plasma = np.sin(x_coords / 40.0) + np.sin(y_coords / 40.0) + np.sin(r / 25.0)
norm = ((plasma + 3.0) / 6.0 * 255.0).astype(np.uint8)
# BGRA channel filling
buf[:, :, 0] = (norm * 0.4).astype(np.uint8) # B
buf[:, :, 1] = (norm * 0.15).astype(np.uint8) # G
buf[:, :, 2] = (norm * 0.25).astype(np.uint8) # R
buf[:, :, 3] = 255 # A
# 2. Blend2D Hardware Vector HUD Overlay
with Context(img) as ctx:
# Crosshairs and radar rings
ctx.stroke_width = 1.5
ctx.stroke_circle(400, 300, 180, "#38BDF866")
ctx.stroke_circle(400, 300, 100, "#38BDF888")
ctx.stroke_circle(400, 300, 40, "#38BDF8AA")
ctx.stroke_line(150, 300, 650, 300, "#38BDF844")
ctx.stroke_line(400, 50, 400, 550, "#38BDF844")
# Rotating target reticle
with ctx.push_state():
ctx.translate(400, 300)
ctx.rotate_deg(45.0)
ctx.stroke_width = 3.0
ctx.stroke_round_rect(-70, -70, 140, 140, 12, "#38BDF8")
# Data markers
font_path = get_font()
if font_path:
font = Font.from_file(font_path, 16.0)
ctx.fill_text(420, 280, font, "TARGET ACQUIRED: NUMPY 0-COPY", "#38BDF8")
ctx.fill_text(420, 320, font, f"RES: {width}x{height} | PRGB32", "#94A3B8")
os.makedirs("output", exist_ok=True)
out_file = "output/05_numpy_vector_overlay.png"
img.write_to_file(out_file)
print(f"Saved: {out_file}")
if __name__ == "__main__":
main()