2016-02-29 18:09:54 +01:00
|
|
|
// Copyright 2016 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-03-14 17:26:21 +01:00
|
|
|
// +build example jsgo
|
2016-08-25 17:40:39 +02:00
|
|
|
|
2016-02-29 18:09:54 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-03-14 17:16:41 +01:00
|
|
|
"bytes"
|
2016-02-29 18:09:54 +01:00
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
_ "image/jpeg"
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten"
|
2018-03-14 17:16:41 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/examples/resources/images"
|
2016-02-29 18:09:54 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
screenWidth = 320
|
|
|
|
screenHeight = 240
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-01-28 17:59:46 +01:00
|
|
|
bgImage *ebiten.Image
|
|
|
|
fgImage *ebiten.Image
|
2018-01-28 18:47:38 +01:00
|
|
|
maskedFgImage *ebiten.Image
|
2016-02-29 18:09:54 +01:00
|
|
|
spotLightImage *ebiten.Image
|
|
|
|
spotLightX = 0
|
|
|
|
spotLightY = 0
|
|
|
|
spotLightVX = 1
|
|
|
|
spotLightVY = 1
|
|
|
|
)
|
|
|
|
|
2018-01-28 17:59:46 +01:00
|
|
|
func init() {
|
2018-03-16 04:05:53 +01:00
|
|
|
// Decode image from a byte slice instead of a file so that
|
|
|
|
// this example works in any working directory.
|
|
|
|
// If you want to use a file, there are some options:
|
|
|
|
// 1) Use os.Open and pass the file to the image decoder.
|
|
|
|
// This is a very regular way, but doesn't work on browsers.
|
|
|
|
// 2) Use ebitenutil.OpenFile and pass the file to the image decoder.
|
|
|
|
// This works even on browsers.
|
|
|
|
// 3) Use ebitenutil.NewImageFromFile to create an ebiten.Image directly from a file.
|
|
|
|
// This also works on browsers.
|
2018-03-14 17:16:41 +01:00
|
|
|
img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
|
2018-01-28 17:59:46 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-03-14 17:16:41 +01:00
|
|
|
bgImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
|
2018-01-28 17:59:46 +01:00
|
|
|
|
2018-03-14 17:16:41 +01:00
|
|
|
img, _, err = image.Decode(bytes.NewReader(images.FiveYears_jpg))
|
2018-01-28 17:59:46 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-03-14 17:16:41 +01:00
|
|
|
fgImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
|
2018-01-28 17:59:46 +01:00
|
|
|
|
2018-02-13 18:55:51 +01:00
|
|
|
maskedFgImage, _ = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterDefault)
|
2018-01-28 17:59:46 +01:00
|
|
|
|
|
|
|
// Initialize the spot light image.
|
|
|
|
const r = 64
|
|
|
|
alphas := image.Point{r * 2, r * 2}
|
|
|
|
a := image.NewAlpha(image.Rectangle{image.ZP, alphas})
|
|
|
|
for j := 0; j < alphas.Y; j++ {
|
|
|
|
for i := 0; i < alphas.X; i++ {
|
|
|
|
// d is the distance between (i, j) and the (circle) center.
|
|
|
|
d := math.Sqrt(float64((i-r)*(i-r) + (j-r)*(j-r)))
|
2018-01-28 18:53:34 +01:00
|
|
|
// Alphas around the center are 0 and values outside of the circle are 0xff.
|
|
|
|
b := uint8(max(0, min(0xff, int(3*d*0xff/r)-2*0xff)))
|
2018-01-28 17:59:46 +01:00
|
|
|
a.SetAlpha(i, j, color.Alpha{b})
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 18:55:51 +01:00
|
|
|
spotLightImage, _ = ebiten.NewImageFromImage(a, ebiten.FilterDefault)
|
2018-01-28 17:59:46 +01:00
|
|
|
}
|
|
|
|
|
2016-02-29 18:09:54 +01:00
|
|
|
func update(screen *ebiten.Image) error {
|
|
|
|
spotLightX += spotLightVX
|
|
|
|
spotLightY += spotLightVY
|
|
|
|
if spotLightX < 0 {
|
|
|
|
spotLightX = -spotLightX
|
|
|
|
spotLightVX = -spotLightVX
|
|
|
|
}
|
|
|
|
if spotLightY < 0 {
|
|
|
|
spotLightY = -spotLightY
|
|
|
|
spotLightVY = -spotLightVY
|
|
|
|
}
|
|
|
|
w, h := spotLightImage.Size()
|
|
|
|
maxX, maxY := screenWidth-w, screenHeight-h
|
|
|
|
if maxX < spotLightX {
|
|
|
|
spotLightX = -spotLightX + 2*maxX
|
|
|
|
spotLightVX = -spotLightVX
|
|
|
|
}
|
|
|
|
if maxY < spotLightY {
|
|
|
|
spotLightY = -spotLightY + 2*maxY
|
|
|
|
spotLightVY = -spotLightVY
|
|
|
|
}
|
2018-01-28 17:59:46 +01:00
|
|
|
|
2017-05-16 03:35:58 +02:00
|
|
|
if ebiten.IsRunningSlowly() {
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-28 17:59:46 +01:00
|
|
|
|
2018-01-28 18:53:34 +01:00
|
|
|
// Reset the maskedFgImage.
|
|
|
|
maskedFgImage.Fill(color.White)
|
2016-02-29 18:09:54 +01:00
|
|
|
op := &ebiten.DrawImageOptions{}
|
2018-01-28 18:53:34 +01:00
|
|
|
op.CompositeMode = ebiten.CompositeModeCopy
|
2016-02-29 18:09:54 +01:00
|
|
|
op.GeoM.Translate(float64(spotLightX), float64(spotLightY))
|
2018-01-28 18:47:38 +01:00
|
|
|
maskedFgImage.DrawImage(spotLightImage, op)
|
2016-02-29 18:09:54 +01:00
|
|
|
|
2018-01-28 18:53:34 +01:00
|
|
|
// Use 'source-in' composite mode so that the source image (fgImage) is used but the alpha
|
|
|
|
// is determined by the destination image (maskedFgImage).
|
2018-01-28 17:59:46 +01:00
|
|
|
//
|
2018-01-28 18:53:34 +01:00
|
|
|
// The result image is the source image with the destination alpha. In maskedFgImage, alpha
|
|
|
|
// values in the hole is 0 and alpha values in other places are 0xff. As a result, the
|
|
|
|
// maskedFgImage draws the source image with a hole that shape is spotLightImage. Note that
|
|
|
|
// RGB values in the destination image are ignored.
|
2018-01-28 17:59:46 +01:00
|
|
|
//
|
2018-01-28 18:53:34 +01:00
|
|
|
// See also https://www.w3.org/TR/compositing-1/#porterduffcompositingoperators_srcin.
|
2016-02-29 18:09:54 +01:00
|
|
|
op = &ebiten.DrawImageOptions{}
|
2018-01-28 18:53:34 +01:00
|
|
|
op.CompositeMode = ebiten.CompositeModeSourceIn
|
2018-01-28 18:47:38 +01:00
|
|
|
maskedFgImage.DrawImage(fgImage, op)
|
2016-02-29 18:09:54 +01:00
|
|
|
|
2017-03-04 15:00:19 +01:00
|
|
|
screen.Fill(color.RGBA{0x00, 0x00, 0x80, 0xff})
|
2018-01-28 17:59:46 +01:00
|
|
|
screen.DrawImage(bgImage, &ebiten.DrawImageOptions{})
|
2018-01-28 18:47:38 +01:00
|
|
|
screen.DrawImage(maskedFgImage, &ebiten.DrawImageOptions{})
|
2016-02-29 18:09:54 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func max(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Masking (Ebiten Demo)"); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|