WLJS LogoWLJS Notebook

Raster

Supports Offload
Raster[data_]

represents a raster (pixel) image that can be placed inside Graphics

data is usually a 2D array of grayscale values ({{a1,a2,...},...}), or a 2D array of color vectors (for example RGB or RGBA {{{r1,g1,b1},{r2,g2,b2},...},...}).

Raster[data_, {{xmin_, ymin_}, {xmax_, ymax_}}]

places the raster into an explicit coordinate region (akin to Rectangle).

while exclusively for grayscale you can specify low and high values mapped to the darkest and brighest pixels:

Raster[data_, {{xmin_, ymin_}, {xmax_, ymax_}}, {low_, high_}]

Examples

ArrayPlot

This plotting function implicitly produces Raster:

ArrayPlot[RandomReal[1, {20, 20}], ColorFunction -> "Rainbow"]

Grayscale matrix

data = Table[Sin[x y]^2, {y, 0, 4 Pi, 0.12}, {x, 0, 4 Pi, 0.12}];

Graphics[
  Raster[data],
  Frame -> True,
  PlotRange -> {{0, Length[data[[1]]]}, {0, Length[data]}}
]

Colored raster (RGBA)

rgba = Table[
   {x, y, 1 - x, 0.8},
   {y, 0.05, 1.0, 0.05},
   {x, 0.05, 1.0, 0.05}
];

Graphics[
 {
   EdgeForm[Black],
   Rectangle[{-0.2, -0.2}, {1.2, 1.2}],
   Raster[rgba, {{0, 0}, {1, 1}}]
 },
 Frame -> True,
 ImagePadding -> 20
]

Overlay raster with vector primitives

img = Table[{x, 0.2 + 0.7 y, 1 - y, 1.0}, {y, 0, 1, 0.03}, {x, 0, 1, 0.03}];

Graphics[
 {
   Raster[img, {{-2, -1}, {2, 1}}],
   White,
   AbsoluteThickness[2],
   Circle[{0, 0}, 0.65],
   Line[{{-2, 0}, {2, 0}}],
   Line[{{0, -1}, {0, 1}}]
 },
 Frame -> True
]

Updates

Raster supports Offload method if position, scaling and array size does not change. For example:

Refresh[Graphics[Raster[RandomReal[{0,1}, {256,256}]]], 1/30.0]

Here is another example with manual controls:

rasterDensityPlotAnim[f_, {xs_, xmin_, xmax_}, {ys_, ymin_, ymax_}, {ts_, tmin_, tmax_}, OptionsPattern[]] := 
 With[{
   time = Unique[], buffer = Unique[], 
   cf = With[{c=ColorData[OptionValue[ColorFunction]]}, (List@@c[#])&], 
   pts = OptionValue[PlotPoints]//N,
   dt = (tmax-tmin)/OptionValue[PlotPoints]//N
  },
  {
   raw := Table[
    f /. {xs -> j, ys -> i, ts -> time} // N, {i, ymin, 
     ymax, (ymax - ymin)/(pts - 1)}, {j, xmin, 
     xmax, (xmax - xmin)/(pts - 1)}]
  },
  
  time = tmin;
   
  buffer = Map[cf, raw // Rescale, {2}];
  
  Graphics[{
    Raster[buffer//Offload, {{xmin, xmax}, {ymin, ymax}}],
    EventHandler[AnimationFrameListener[buffer//Offload], Function[Null,
      buffer = Map[cf, raw // Rescale, {2}];
      time = If[time >= tmax, tmin, time + dt];
    ]]
  }, Frame->True]
]

Options[rasterDensityPlotAnim] = {ColorFunction->"AvocadoColors", PlotPoints->100};
rasterDensityPlotAnim[x Sin[y+t], {x, -5, 5}, {y, 0, 10}, {t, 0, 2Pi}, PlotPoints->50]
Using NumericArray may improve the performance

On this page