From 93e9ab79b16a466a4b168f0d9b0419c899fc1f56 Mon Sep 17 00:00:00 2001 From: faiface Date: Sat, 15 Apr 2017 17:10:35 +0200 Subject: [PATCH] add xor example --- examples/xor/README.md | 6 +++ examples/xor/main.go | 76 ++++++++++++++++++++++++++++++++++++ examples/xor/screenshot.png | Bin 0 -> 10628 bytes 3 files changed, 82 insertions(+) create mode 100644 examples/xor/README.md create mode 100644 examples/xor/main.go create mode 100644 examples/xor/screenshot.png diff --git a/examples/xor/README.md b/examples/xor/README.md new file mode 100644 index 0000000..a71e139 --- /dev/null +++ b/examples/xor/README.md @@ -0,0 +1,6 @@ +# Xor + +This example demonstrates an unusual Porter-Duff composition method: Xor. (And the capability of +drawing circles.) + +![Screenshot](screenshot.png) \ No newline at end of file diff --git a/examples/xor/main.go b/examples/xor/main.go new file mode 100644 index 0000000..6cefb07 --- /dev/null +++ b/examples/xor/main.go @@ -0,0 +1,76 @@ +package main + +import ( + "math" + "time" + + "github.com/faiface/pixel" + "github.com/faiface/pixel/imdraw" + "github.com/faiface/pixel/pixelgl" + "golang.org/x/image/colornames" +) + +func run() { + cfg := pixelgl.WindowConfig{ + Title: "Xor", + Bounds: pixel.R(0, 0, 1024, 768), + Resizable: true, + VSync: true, + } + win, err := pixelgl.NewWindow(cfg) + if err != nil { + panic(err) + } + + imd := imdraw.New(nil) + + canvas := pixelgl.NewCanvas(win.Bounds()) + + start := time.Now() + for !win.Closed() { + // in case window got resized, we also need to resize out canvas + canvas.SetBounds(win.Bounds()) + + offset := math.Sin(time.Since(start).Seconds()) * 300 + + // clear the canvas to be totally transparent and set the xor compose method + canvas.Clear(pixel.Alpha(0)) + canvas.SetComposeMethod(pixel.ComposeXor) + + // red circle + imd.Clear() + imd.Color(pixel.RGB(1, 0, 0)) + imd.Push(pixel.X(-offset) + win.Bounds().Center()) + imd.Circle(200, 0) + imd.Draw(canvas) + + // blue circle + imd.Clear() + imd.Color(pixel.RGB(0, 0, 1)) + imd.Push(pixel.X(offset) + win.Bounds().Center()) + imd.Circle(150, 0) + imd.Draw(canvas) + + // yellow circle + imd.Clear() + imd.Color(pixel.RGB(1, 1, 0)) + imd.Push(pixel.Y(-offset) + win.Bounds().Center()) + imd.Circle(100, 0) + imd.Draw(canvas) + + // magenta circle + imd.Clear() + imd.Color(pixel.RGB(1, 0, 1)) + imd.Push(pixel.Y(offset) + win.Bounds().Center()) + imd.Circle(50, 0) + imd.Draw(canvas) + + win.Clear(colornames.Green) + canvas.Draw(win) + win.Update() + } +} + +func main() { + pixelgl.Run(run) +} diff --git a/examples/xor/screenshot.png b/examples/xor/screenshot.png new file mode 100644 index 0000000000000000000000000000000000000000..3b96dd9d7d842094e9c0c680c0a401d7c5d46338 GIT binary patch literal 10628 zcmeHtX;f3^*6=cGRffo*$PhsYkmP*((7V=q@4Dam*1OiX-d}I?BRtPJdwTY> z=aBB_<#qZBzE4aSpTSR zW8>Cs3xn{jQCq^MZV%lS7PfVJL{w~-med2l0v3AB3D}j}@iy{OSX@GVW4(t*Xln0r zGp^e*r_jxz-XF@B?$?i5`0*Ck@9yHD+ytdBGvZN)_855 zQSDW^?m1oI>biH=cK!a9)wJTlj~B&F*Yeb#YJ%gNkBUFPyc#-|K4$Ha*3)}Bd#$r) ze@=}`>^a%BtL;o`qMKEs&}z)JUF%d-Z9@Os{zG+A&6g;bgu&Qc*XKS~VH>zm2P}4C?=_?oQ+F*3%hI;g9aF zzdAAD4ziV*j>#zR%u+{Qx!rPf<(ioC^gUg7`WIijSWr3`HCx$`Bvq+;YHAh*+aR#u zQfTFYsR)@P_1M#}k7a)Q<1ZJlMAz~0-JsLS>ZZn2e&U9mpWojPzj5=X{Z|QdXbvu? z#hQWeZ_oRjnzg;9%A}G)fhGQ{SFg^PDj&w&*;4i6s}&zI5Ev@#=x-5&LI1I_pudMV zu?;t=r(>H2$8F8*e>bRE_0^bSN;NWAwQP;JEGy<|_07`vd0H^QMn&$EDnkph_~MR8 zxm5kA>#lUDUuw+dBLDs3-F1~sFRymo>PW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