Notebook - Welcome to Notebook

Contact/Report Bugs
You can contact me at: bkenwright@xbdev.net












Complete Tetris game Canvas JS DOCTYPE html html lang en head meta charset UTF 8 style body height 700px canvas position absolute top 45 left 50 width 640px height 640px margin 320px 0 0 320px style head body canvas canvas script use strict var canvas document querySelector canvas canvas width 640 canvas height 640 var g canvas getContext 2d var right x 1 y 0 var down x 0 y 1 var left x 1 y 0 var EMPTY 1 var BORDER 2 var fallingShape var nextShape var dim 640 var nRows 18 var nCols 12 var blockSize 30 var topMargin 50 var leftMargin 20 var scoreX 400 var scoreY 330 var titleX 130 var titleY 160 var clickX 120 var clickY 400 var previewCenterX 467 var previewCenterY 97 var mainFont bold 48px monospace var smallFont bold 18px monospace var colors green red blue purple orange blueviolet magenta var gridRect x 46 y 47 w 308 h 517 var previewRect x 387 y 47 w 200 h 200 var titleRect x 100 y 95 w 252 h 100 var clickRect x 50 y 375 w 252 h 40 var outerRect x 5 y 5 w 630 h 630 var squareBorder white var titlebgColor white var textColor black var bgColor DDEEFF var gridColor BECFEA var gridBorderColor 7788AA var largeStroke 5 var smallStroke 2 position of falling shape var fallingShapeRow var fallingShapeCol var keyDown false var fastDown false var grid var scoreboard new Scoreboard addEventListener keydown function event if keyDown keyDown true if scoreboard isGameOver return switch event key case w case ArrowUp if canRotate fallingShape rotate fallingShape break case a case ArrowLeft if canMove fallingShape left move left break case d case ArrowRight if canMove fallingShape right move right break case s case ArrowDown if fastDown fastDown true while canMove fallingShape down move down draw shapeHasLanded draw addEventListener click function startNewGame addEventListener keyup function keyDown false fastDown false function canRotate s if s Shapes Square return false var pos new Array 4 for var i 0 i pos length i pos i s pos i slice pos forEach function row var tmp row 0 row 0 row 1 row 1 tmp return pos every function p var newCol fallingShapeCol p 0 var newRow fallingShapeRow p 1 return grid newRow newCol EMPTY function rotate s if s Shapes Square return s pos forEach function row var tmp row 0 row 0 row 1 row 1 tmp function move dir fallingShapeRow dir y fallingShapeCol dir x function canMove s dir return s pos every function p var newCol fallingShapeCol dir x p 0 var newRow fallingShapeRow dir y p 1 return grid newRow newCol EMPTY function shapeHasLanded addShape fallingShape if fallingShapeRow 2 scoreboard setGameOver scoreboard setTopscore else scoreboard addLines removeLines selectShape function removeLines var count 0 for var r 0 r nRows 1 r for var c 1 c nCols 1 c if grid r c EMPTY break if c nCols 2 count removeLine r return count function removeLine line for var c 0 c nCols c grid line c EMPTY for var c 0 c nCols c for var r line r 0 r grid r c grid r 1 c function addShape s s pos forEach function p grid fallingShapeRow p 1 fallingShapeCol p 0 s ordinal function Shape shape o this shape shape this pos this reset this ordinal o var Shapes ZShape 0 1 0 0 1 0 1 1 SShape 0 1 0 0 1 0 1 1 IShape 0 1 0 0 0 1 0 2 TShape 1 0 0 0 1 0 0 1 Square 0 0 1 0 0 1 1 1 LShape 1 1 0 1 0 0 0 1 JShape 1 1 0 1 0 0 0 1 function getRandomShape var keys Object keys Shapes var ord Math floor Math random keys length var shape Shapes keys ord return new Shape shape ord Shape prototype reset function this pos new Array 4 for var i 0 i this pos length i this pos i this shape i slice return this pos function selectShape fallingShapeRow 1 fallingShapeCol 5 fallingShape nextShape nextShape getRandomShape if fallingShape null fallingShape reset function Scoreboard this MAXLEVEL 9 var level 0 var lines 0 var score 0 var topscore 0 var gameOver true this reset function this setTopscore level lines score 0 gameOver false this setGameOver function gameOver true this isGameOver function return gameOver this setTopscore function if score topscore topscore score this getTopscore function return topscore this getSpeed function switch level case 0 return 700 case 1 return 600 case 2 return 500 case 3 return 400 case 4 return 350 case 5 return 300 case 6 return 250 case 7 return 200 case 8 return 150 case 9 return 100 default return 100 this addScore function sc score sc this addLines function line switch line case 1 this addScore 10 break case 2 this addScore 20 break case 3 this addScore 30 break case 4 this addScore 40 break default return lines line if lines 10 this addLevel this addLevel function lines 10 if level this MAXLEVEL level this getLevel function return level this getLines function return lines this getScore function return score function draw g clearRect 0 0 canvas width canvas height drawUI if scoreboard isGameOver drawStartScreen else drawFallingShape function drawStartScreen g font mainFont fillRect titleRect titlebgColor fillRect clickRect titlebgColor g fillStyle textColor g fillText Tetris titleX titleY g font smallFont g fillText click to start clickX clickY function fillRect r color g fillStyle color g fillRect r x r y r w r h function drawRect r color g strokeStyle color g strokeRect r x r y r w r h function drawSquare colorIndex r c var bs blockSize g fillStyle colors colorIndex g fillRect leftMargin c bs topMargin r bs bs bs g lineWidth smallStroke g strokeStyle squareBorder g strokeRect leftMargin c bs topMargin r bs bs bs function drawUI background fillRect outerRect bgColor fillRect gridRect gridColor the blocks dropped in the grid for var r 0 r nRows r for var c 0 c nCols c var idx grid r c if idx EMPTY drawSquare idx r c the borders of grid and preview panel g lineWidth largeStroke drawRect gridRect gridBorderColor drawRect previewRect gridBorderColor drawRect outerRect gridBorderColor scoreboard g fillStyle textColor g font smallFont g fillText hiscore scoreboard getTopscore scoreX scoreY g fillText level scoreboard getLevel scoreX scoreY 30 g fillText lines scoreboard getLines scoreX scoreY 60 g fillText score scoreboard getScore scoreX scoreY 90 preview var minX 5 minY 5 maxX 0 maxY 0 nextShape pos forEach function p minX Math min minX p 0 minY Math min minY p 1 maxX Math max maxX p 0 maxY Math max maxY p 1 var cx previewCenterX minX maxX 1 2 0 blockSize var cy previewCenterY minY maxY 1 2 0 blockSize g translate cx cy nextShape shape forEach function p drawSquare nextShape ordinal p 1 p 0 g translate cx cy function drawFallingShape var idx fallingShape ordinal fallingShape pos forEach function p drawSquare idx fallingShapeRow p 1 fallingShapeCol p 0 function animate lastFrameTime var requestId requestAnimationFrame function animate lastFrameTime var time new Date getTime var delay scoreboard getSpeed if lastFrameTime delay time if scoreboard isGameOver if canMove fallingShape down move down else shapeHasLanded draw lastFrameTime time else cancelAnimationFrame requestId function startNewGame initGrid selectShape scoreboard reset animate 1 function initGrid function fill arr value for var i 0 i arr length i arr i value for var r 0 r nRows r grid r new Array nCols fill grid r EMPTY for var c 0 c nCols c if c 0 c nCols 1 r nRows 1 grid r c BORDER function init initGrid selectShape draw init script body html

et function Scoreboard this MAXLEVEL 9 var level 0 var lines 0 var score 0 var topscore 0 var gameOver true this reset function this setTopscore level lines score 0 gameOver false this setGameOver function gameOver true this isGameOver function return gameOver this setTopscore function if score topscore topscore score this getTopscore function return topscore this getSpeed function switch level case 0 return 700 case 1 return 600 case 2 return 500 case 3 return 400 case 4 return 350 case 5 return 300 case 6 return 250 case 7 return 200 case 8 return 150 case 9 return 100 default return 100 this addScore function sc score sc this addLines function line switch line case 1 this addScore 10 break case 2 this addScore 20 break case 3 this addScore 30 break case 4 this addScore 40 break default return lines line if lines 10 this addLevel this addLevel function lines 10 if level this MAXLEVEL level this getLevel function return level this getLines function return lines this getScore function return score function draw g clearRect 0 0 canvas width canvas height drawUI if scoreboard isGameOver drawStartScreen else drawFallingShape function drawStartScreen g font mainFont fillRect titleRect titlebgColor fillRect clickRect titlebgColor g fillStyle textColor g fillText Tetris titleX titleY g font smallFont g fillText click to start clickX clickY function fillRect r color g fillStyle color g fillRect r x r y r w r h function drawRect r color g strokeStyle color g strokeRect r x r y r w r h function drawSquare colorIndex r c var bs blockSize g fillStyle colors colorIndex g fillRect leftMargin c bs topMargin r bs bs bs g lineWidth smallStroke g strokeStyle squareBorder g strokeRect leftMargin c bs topMargin r bs bs bs function drawUI background fillRect outerRect bgColor fillRect gridRect gridColor the blocks dropped in the grid for var r 0 r nRows r for var c 0 c nCols c var idx grid r c if idx EMPTY drawSquare idx r c the borders of grid and preview panel g lineWidth largeStroke drawRect gridRect gridBorderColor drawRect previewRect gridBorderColor drawRect outerRect gridBorderColor scoreboard g fillStyle textColor g font smallFont g fillText hiscore scoreboard getTopscore scoreX scoreY g fillText level scoreboard getLevel scoreX scoreY 30 g fillText lines scoreboard getLines scoreX scoreY 60 g fillText score scoreboard getScore scoreX scoreY 90 preview var minX 5 minY 5 maxX 0 maxY 0 nextShape pos forEach function p minX Math min minX p 0 minY Math min minY p 1 maxX Math max maxX p 0 maxY Math max maxY p 1 var cx previewCenterX minX maxX 1 2 0 blockSize var cy previewCenterY minY maxY 1 2 0 blockSize g translate cx cy nextShape shape forEach function p drawSquare nextShape ordinal p 1 p 0 g translate cx cy function drawFallingShape var idx fallingShape ordinal fallingShape pos forEach function p drawSquare idx fallingShapeRow p 1 fallingShapeCol p 0 function animate lastFrameTime var requestId requestAnimationFrame function animate lastFrameTime var time new Date getTime var delay scoreboard getSpeed if lastFrameTime delay time if scoreboard isGameOver if canMove fallingShape down move down else shapeHasLanded draw lastFrameTime time else cancelAnimationFrame requestId function startNewGame initGrid selectShape scoreboard reset animate 1 function initGrid function fill arr value for var i 0 i arr length i arr i value for var r 0 r nRows r grid r new Array nCols fill grid r EMPTY for var c 0 c nCols c if c 0 c nCols 1 r nRows 1 grid r c BORDER function init initGrid selectShape draw init script body html

3dplot
a4print
about
acejs
acejs2
acejs3
aessecurity
angularjs
animbackgroundimage
aseformat
assert
asteroidsjs
backgrounds01
backgrounds02
backgrounds03
barnsleyfern
base26
base64
bib
binary
bodypix
bouncy
box2dweb
breakoutjs
browserversion
buslanes
busybutton
bvhreader
calendar
canvas
canvas2
canvas3
canvasmandelbrot
canvasmandelbrot2
canvasnumbers
canvaszoom
capsule
changingimages
chatgptusingopenai
chatgptusingopenai2
chatgptusingopenai3
checkboxtoggle
chinesetiles
classes
classfeatures
clipboardbutton
clonenode
codedropdown
codemirror
codemirror2
collada
colorpick
columnresizer
contextmenu
convnet
cookiebanner
countdown
countdown2
countdown3
crop
css3dbarchart
css3dbarchart2
css3dbook
css3dscene
csscube
csscube2
csscube3
csscubevideos
cssfilelist
csshas
csspulse
cssresizeaspect
cssspin
csszooming
csvtoarray
customcheckbox
d3datamap
d3js
d3js10
d3js11
d3js2
d3js3
d3js4
d3js5
d3js6
d3js7
d3js8
d3js9
d3jsanimatedgrid
d3jsarctransition
d3jsarctransition2
d3jsaxis
d3jsaxischanging
d3jsbars
d3jsbrushing
d3jsbuslanes
d3jsbuslanes2
d3jscalendar
d3jscheat
d3jsclock
d3jscloudmap
d3jscogs
d3jscolors
d3jscovid
d3jscovid2
d3jscovid3
d3jsdashboard
d3jsdashboard2
d3jsdashboard3
d3jsdatakeyfunction
d3jsdensity
d3jsdragresizing
d3jsdragresizing2
d3jseach
d3jsease
d3jsevents
d3jsflower
d3jsforcegroups
d3jsforces
d3jsforces2
d3jsfractaltree
d3jsgeo
d3jsgroupbars
d3jsgroups
d3jsheatmap
d3jshex
d3jshierarchies
d3jshierarchies2
d3jshistogram
d3jshistogram2
d3jshistogram3
d3jshistogram4
d3jsinterpolate
d3jsjoin
d3jskmean
d3jskmean2
d3jsline
d3jsline2
d3jsline3
d3jsline4
d3jslinetransition
d3jslinetransition0
d3jslinetransition2
d3jsmaplocations
d3jsmaps
d3jsmaps2
d3jsmaps3
d3jsmisc
d3jsmisc2
d3jsmodule
d3jsmodulecolor
d3jsmultistyles
d3jsnobel
d3jsoverlappinggraphs
d3jspanel
d3jspie
d3jspieinterpolate
d3jssankey
d3jssankey2
d3jsscatter
d3jsshapes
d3jsslider
d3jsspending
d3jsspending2
d3jsspiralplot
d3jsspirograph
d3jssquare
d3jsstack
d3jsstackedbar
d3jsstackedbar2
d3jssunburst
d3jssunmoon
d3jssvglines
d3jssymbols
d3jstimelines
d3jsuk
d3jsvoronoi
d3scatterplot
d3timeline
d3timeline2
datalist
datamuse
date
dblclickhighlight
deviceorientation
dictionaryapi
dockermenu
doodlepad
downloadgif
dragdroplistitems
dragrotateresizediv
dragrotateresizediv2
dragrotateresizediv3
dragrotateresizediv4
dragrotateresizefontsize
dragselectbrush
drawlinesdiv
dropdown
dualquaternionimages
dynamicgrid
easefunctions
easeinterpolate3dplots
echart
echart2
echart3
encapsulation
epubviewer
errorstack
excalidraw
excalidraw2
excalidraw3
excalidraw5
expandable
faker
fetchplus
fileupload
fixedtopbar
fonts
fonts2
footerbar
fractalmaze
fractalmaze2
fractalnoiseimage
fractals
fractals2
fractaltree
freesvg
fresnel
froggerjs
gantt
gifgiphyapi
gifhex
gltffromscratch
gradients
griditems
griditems2
griditems3
griditems4
gridworms
heat
hexview
hexview2
highlight
icons
icons2
iframes
ik
imagetracertosvg
imgur
inputfile
invadersjs
ipynb
ipynb2
ipynb3
ipynb4
isbn13
isbn2
jpghex
jquery
jquery2
jqueryui
jqueryui2
jsdraganddrop
jslint
jsobfuscate
jsraytracer
jstree
jstree2
jszip
jszipimages
jszipread
keyframes
l2dwidget
lda
leftmenu
less
less2
lineargradientimage
linenumbers
loadimagefromfile
makepdf
maps
markdown
markdown2
markdownalerts
markdownalerts2
markdownbookmarks
markovimage
markovpixelblocks
mathjax
matrices
matsandvects
mazegamejs
md2tex
metrotiles
metrowindows
milestones
misc
misc2
modules
myipdetails
neataptic
networkstructures
networkstructures2
number
obj
objtojson
openaiimages
opencv
opencv2
opencv3
opencv4
opencv5
outline
p2
p5fractalleaf
p5fractalshape
p5js
p5js2
p5js3
p5jsanimatedcover
p5mengercube
p5snowflakes
palindrome
panel
parallax
paste
paste2
pasteimgfromurl
pdfjs
pdfjs2
pdfkit
pdfkit2
pdfkit3
pdfkit4
pdfkit5
pdfkit6
pdfmake
pdfmake2
pdfmake3
pdfmake4
pdfmake5
pdfmake6
perlin
perlin2
perlin3
perspective
pexels
playground
plotly
plotlynoise
plotlyranddist
plyloader
plyloader2
pngtxtencoder
pongjs
pptxgenjs
prettycode
prism
prn
problems
progress
pseudorandom
px2svg
python
quotes
racergame
random
randomprofilepatterns
randomstring
rating
rayambient
raymonte
raymonteprogressive
raymonteprogressive2
raymontewarmstart
reexpcross
reexpcross2
regex
regexbib
regexpfixbib
regexpmultiline
repeatwordsregexp
resizabletable
resizabletable2
revealjs
revealjs2
revealjsmulti
ritalanguage
ritalanguage2
ritalanguage3
rotateimg
rough
rsapublicprivatekeys
rss
rss2
sankey
scrappingsvg
scrolltext
scrolltext2
scrollwidth
sdfboxinboxtwist
sdfhollowbox
setintervalexception
shareurl
shuffle
sidecomment
similarity
simplehighlighter
simpleplatformgamejs
sinecanvas
sliderpopout
slides
smileys
snowfall
snowman
sound
soundsignal
sphererayintersection
springs
sqljs
steganography
stereogram
stringmatching
svg
svgchaos
svgdragresize
svgdragresize2
svgdragresize3
svgdragrotate
svgdrawing
svglines
svglines2
svglines3
svglines4
svglines5
svglinesmandelbrot
svgpathsdragrotate
svgpathsdragrotateresize
svgpie
svgpie2
svgpie3
svgpiepath
svgpiepath2
svgrandomfaces
symbols
synaptic
synaptic2
synonyms
tablerotatecells
tablerotatecells2
tablerotatecells3
tablerotatecells3b
tablerotatecells4
tables
tablezebra
tabularjs
tabularjs2
tabulatordownload
tagcanvas
tensorflowgan
tensorflowjs
tensorflowjsbasic
tensorflowjscnn
tensorflowjssinewave
tensorflowjssound
tensorflowmobilenet
tetrahedronfractal
tetrahedronfractalfolding
tetris
textarea
textareaauto
textareadiv
textareadiv2
textmaskimage
theirorthere
thesaurus
threejs
threejs2
threejs3
threejs4
threejsgltf
threejstokyo
tiles
toaster
tooltip
transition
transitionexpandabledropdown
treeview
treeview2
tricks
tshirt
tshirt2
tshirt3
turningpages
unsplash
urlblob
urlblob2
userdefinepoints
vector
videos
videos2
visualsort
vue
w2ui
w2uientertextdialog
webcam
webgl
webgl2
webgl3
webgl4
webgl5
webglbasic1
webglbasic2
webglcube
webglfov
webglfrustum
webgljson
webglleaves
webgllighting
webglorthographic
webglpoints1
webglpoints2
webglpoints3
webglsquare
webgltexture1
webgltexture2
webgltexture3
webgltransforms
webgltriangle
webgpu
webgpu10
webgpu11
webgpu12
webgpu13
webgpu14
webgpu15
webgpu16
webgpu17
webgpu2
webgpu3
webgpu4
webgpu5
webgpu6
webgpu7
webgpu8
webgpu9
webgpubars
webgpubuffers
webgpubuffers2
webgpucellnoise
webgpuclouds
webgpuclydescope
webgpucompute
webgpucubemap
webgpucubemap2
webgpudeferred
webgpudepth
webgpudof
webgpudrops
webgpuetha
webgpufire
webgpufractalcubes
webgpuglassrain
webgpugltf
webgpugltf2
webgpugrass
webgpugrid
webgpukernel
webgpukleinian
webgpulabupdates
webgpulighting
webgpumandelbrot
webgpumeta3d
webgpumetaballs
webgpumouse
webgpunoise
webgpunormalmapping
webgpuobj
webgpuparallax
webgpuparallax2
webgpuparallax3
webgpuparallaxshadow
webgpuparallaxshadow2
webgpupixel
webgpuquad
webgpuray1
webgpuraytracing
webgpuraytracing2
webgpushadowmaps
webgpushadowmaps2
webgpusierpinski2d
webgpusierpinski3d
webgpusinusoid
webgpussao
webgpustadiumobj
webgpuswirl
webgputestpipe3
webgputoon
webgputopology
webgputt
webgpuvolcloud
webgpuwater
webgpuwireframe
webgpuwireframe2
webpcanvas
webworkers
webxr
webxr2
wiggly
wikipedia