How I created dynamic lower thirds with chroma key using Phaser

Who said Phaser can only be used to make games? I am going to tell you tell story of a video project, and how Phaser helped me to work with streaming video. As a company, we are making a continue event streaming on YouTube featuring webcams placed around Venice, panoramic videos of the city taken with a drone, time lapse videos with cameras placed for a day or two in beautiful spots, and some more stuff related to tourism and the promotion of Venice. We are using XSplit to broadcast and automatically switch among the various videos with a plugin we developed. But we also needed something to create quality lower thirds with contents dynamically loaded from a database of events and sponsors. From Wikipedia: In the television industry, a lower third is a graphic overlay placed in the title-safe lower area of the screen, though not necessarily the entire lower third of it, as the name suggests. In its simplest form, a lower third can just be text overlying the video. Frequently this text is white with a drop shadow to make the words easier to read. A lower third can also contain graphical elements such as boxes, images or shading. Some lower thirds have animated backgrounds and text. Our lower third will be placed on the video output using the Chroma Key technique. From Wikipedia again: Chroma key compositing, or chroma keying, is a visual effects / post-production technique for compositing (layering) two images or video streams together based on color hues (chroma range). The technique has been used heavily in many fields to remove a background from the subject of a photo or video – particularly the newscasting, motion picture and videogame industries. There are some software products which create lower thirds on the fly, but they are expensive and it’s not easy to connect them with a MySQL database, so here’s the plan: a green Phaser “game” with the lower third animated with tweens and bitmap fonts. While I can’t show you the entire project at the moment – it isn’t even ready – I am showing you the lower thirds we are working on:
The green background will then be used as chroma key to releveal underneath video, and everything will work fine. I will show you the complete project once it’s done. Here is the source code:
var game;

window.onload = function() {
    game = new Phaser.Game(1920, 1080, Phaser.CANVAS);
    game.state.add("Play", play);
    game.state.start("Play");
}

var play = function(){}
play.prototype = {
    preload: function(){
        game.load.image("mainthird", "mainthird.png");
        game.load.image("lowerthird", "lowerthird.png");
        game.load.bitmapFont("font", "font.png", "font.fnt");
    },
    create: function(){
        game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
        game.scale.pageAlignHorizontally = true;
        game.scale.pageAlignVertically = true;
        game.stage.disableVisibilityChange = true;
        game.stage.backgroundColor = 0x00ff00;
        this.lowerthird = game.add.sprite(650, game.height - 400, "lowerthird");
        this.lowerthird.anchor.set(0.5);
        this.lowerthird.visible = false;
        this.mainthird = game.add.sprite(0, game.height - 400, "mainthird");
        this.mainthird.scale.x = 0.1;
        this.mainthird.scale.y = 0.1;
        this.mainthird.anchor.set(0.5);
        var tween = game.add.tween(this.mainthird).to({
            x: 850
        }, 200, Phaser.Easing.Linear.None, true);
        tween.onComplete.add(function(){
            var tween = game.add.tween(this.mainthird.scale).to({
                x: 1,
                y: 1
            }, 600, Phaser.Easing.Cubic.InOut, true);
            tween.onComplete.add(function(){
                var mainText = game.add.bitmapText(140, game.height - 390, "font", "Palazzo Grassi - From Big Moghul to Maharaja", 72);
                mainText.anchor.set(0, 0.5);
                this.lowerthird.visible = true;
                var tween = game.add.tween(this.lowerthird).to({
                    y: game.height - 320
                }, 700, Phaser.Easing.Bounce.Out, true);
                tween.onComplete.add(function(){
                    var upperText = game.add.bitmapText(180, game.height - 285, "font", "TODAY 8 AM - 5 PM", 60);
                    upperText.anchor.set(0, 0.5);
                }, this)
            }, this)
        }, this)
        game.time.events.add(Phaser.Timer.SECOND * 10, function(){
            game.state.start("Play");
        }, this);
    }
}
It’s easy to see how you can dynamically load texts with PHP/MySQL and that’s it! We have our lower third generator at no cost. Download the source code.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers