style h2 p margin 0 padding 0 body padding 10px margin 10px background color 444 color 333 font family Helvetica sans serif height 400px book background url https notebook xbdev net var images book png no repeat position absolute width 830px height 260px left 50 top 50 transform translate 50 50 margin left 400px margin top 125px pages section background url https notebook xbdev net var images paper png no repeat display block width 400px height 250px position absolute left 415px top 5px overflow hidden pages section div display block width 300px height 250px font size 12px pages section p pages section h2 padding 3px 35px line height 1 4em text align justify pages section h2 margin 15px 0 10px canvaspage position absolute z index 100 style div id book canvas id canvaspage canvas div id pages section div h2 History h2 p Canvas was initially introduced by Apple for use inside their own Mac OS X WebKit component powering applications like Dashboard widgets and the Safari browser Later it was adopted by Gecko browsers and Opera and standardized by the WHATWG on new proposed specifications for next generation web technologies p div section section div h2 Usage h2 p Canvas consists of a drawable region defined in HTML code with height and width attributes JavaScript code may access the area through a full set of drawing functions similar to other common 2D APIs thus allowing for dynamically generated graphics Some anticipated uses of canvas include building graphs animations games and image composition p div section section div h2 Reactions h2 p At the time of its introduction the canvas element was met with mixed reactions from the web standards community There have been arguments against Apple s decision to create a new proprietary element instead of supporting the SVG standard There are other concerns about syntax e g the absence of a namespace p div section section div h2 Support h2 p The element is currently supported by the latest versions of Mozilla Firefox Google Chrome Safari and Opera It is not natively implemented by Internet Explorer IE as of version 8 though support is in development for Internet Explorer 9 however many of the Canvas element s features can be supported in IE for example by using Google or Mozilla plugins JavaScript libraries and either Adobe Flash or IE s proprietary VML p div section div div script Dimensions of the whole book var BOOK_WIDTH 830 var BOOK_HEIGHT 260 Dimensions of one page in the book var PAGE_WIDTH 400 var PAGE_HEIGHT 250 Vertical spacing between the top edge of the book and the papers var PAGE_Y BOOK_HEIGHT PAGE_HEIGHT 2 The canvas size equals to the book dimensions this padding var CANVAS_PADDING 60 var page 0 var canvas document getElementById canvaspage var context canvas getContext 2d var mouse x 0 y 0 var flips var book document getElementById book List of all the page elements in the DOM var pages book getElementsByTagName section Organize the depth of our pages and create the flip definitions for var i 0 len pages length i len i pages i style zIndex len i flips push Current progress of the flip left 1 to right 1 progress 1 The target value towards which progress is always moving target 1 The page DOM element related to this flip page pages i True while the page is being dragged dragging false Resize the canvas to match the book size canvas width BOOK_WIDTH CANVAS_PADDING 2 canvas height BOOK_HEIGHT CANVAS_PADDING 2 Offset the canvas so that it s padding is evenly spread around the book canvas style top CANVAS_PADDING px canvas style left CANVAS_PADDING px Render the page flip 60 times a second setInterval render 1000 60 document addEventListener mousemove mouseMoveHandler false document addEventListener mousedown mouseDownHandler false document addEventListener mouseup mouseUpHandler false function mouseMoveHandler event Offset mouse position so that the top of the book spine is 0 0 mouse x event clientX book offsetLeft BOOK_WIDTH 2 mouse y event clientY book offsetTop function mouseDownHandler event Make sure the mouse pointer is inside of the book if Math abs mouse x PAGE_WIDTH if mouse x 0 page 1 0 We are on the left side drag the previous page flips page 1 dragging true else if mouse x 0 page 1 flips length We are on the right side drag the current page flips page dragging true Prevents the text selection event preventDefault function mouseUpHandler event for var i 0 i flips length i If this flip was being dragged animate to its destination if flips i dragging Figure out which page we should navigate to if mouse x 0 flips i target 1 page Math min page 1 flips length else flips i target 1 page Math max page 1 0 flips i dragging false function render Reset all pixels in the canvas context clearRect 0 0 canvas width canvas height for var i 0 len flips length i len i var flip flips i if flip dragging flip target Math max Math min mouse x PAGE_WIDTH 1 1 Ease progress towards the target value flip progress flip target flip progress 0 2 If the flip is being dragged or is somewhere in the middle of the book render it if flip dragging Math abs flip progress 0 997 drawFlip flip function drawFlip flip Strength of the fold is strongest in the middle of the book var strength 1 Math abs flip progress Width of the folded paper var foldWidth PAGE_WIDTH 0 5 1 flip progress X position of the folded paper var foldX PAGE_WIDTH flip progress foldWidth How far the page should outdent vertically due to perspective var verticalOutdent 20 strength The maximum width of the left and right side shadows var paperShadowWidth PAGE_WIDTH 0 5 Math max Math min 1 flip progress 0 5 0 var rightShadowWidth PAGE_WIDTH 0 5 Math max Math min strength 0 5 0 var leftShadowWidth PAGE_WIDTH 0 5 Math max Math min strength 0 5 0 Change page element width to match the x position of the fold flip page style width Math max foldX 0 px context save context translate CANVAS_PADDING BOOK_WIDTH 2 PAGE_Y CANVAS_PADDING Draw a sharp shadow on the left side of the page context strokeStyle rgba 0 0 0 0 05 strength context lineWidth 30 strength context beginPath context moveTo foldX foldWidth verticalOutdent 0 5 context lineTo foldX foldWidth PAGE_HEIGHT verticalOutdent 0 5 context stroke Right side drop shadow var rightShadowGradient context createLinearGradient foldX 0 foldX rightShadowWidth 0 rightShadowGradient addColorStop 0 rgba 0 0 0 strength 0 2 rightShadowGradient addColorStop 0 8 rgba 0 0 0 0 0 context fillStyle rightShadowGradient context beginPath context moveTo foldX 0 context lineTo foldX rightShadowWidth 0 context lineTo foldX rightShadowWidth PAGE_HEIGHT context lineTo foldX PAGE_HEIGHT context fill Left side drop shadow var leftShadowGradient context createLinearGradient foldX foldWidth leftShadowWidth 0 foldX foldWidth 0 leftShadowGradient addColorStop 0 rgba 0 0 0 0 0 leftShadowGradient addColorStop 1 rgba 0 0 0 strength 0 15 context fillStyle leftShadowGradient context beginPath context moveTo foldX foldWidth leftShadowWidth 0 context lineTo foldX foldWidth 0 context lineTo foldX foldWidth PAGE_HEIGHT context lineTo foldX foldWidth leftShadowWidth PAGE_HEIGHT context fill Gradient applied to the folded paper highlights shadows var foldGradient context createLinearGradient foldX paperShadowWidth 0 foldX 0 foldGradient addColorStop 0 35 fafafa foldGradient addColorStop 0 73 eeeeee foldGradient addColorStop 0 9 fafafa foldGradient addColorStop 1 0 e2e2e2 context fillStyle foldGradient context strokeStyle rgba 0 0 0 0 06 context lineWidth 0 5 Draw the folded piece of paper context beginPath context moveTo foldX 0 context lineTo foldX PAGE_HEIGHT context quadraticCurveTo foldX PAGE_HEIGHT verticalOutdent 2 foldX foldWidth PAGE_HEIGHT verticalOutdent context lineTo foldX foldWidth verticalOutdent context quadraticCurveTo foldX verticalOutdent 2 foldX 0 context fill context stroke context restore console log ready script let canvas document createElement canvas canvas width 720 canvas height 480 document body appendChild canvas n canvas with n c getContext 2d width w 720 height h 480 g X Y x y i o 0 z 66 B d function a b c d return a c a c b d b d document onmousedown function e if d X Y x y w g 1 style cursor se resize r document onmouseup function e style cursor default g 0 if d w 3 h 3 x y 83200 x y 0 z v document onmousemove function e X e pageX 1 Y e pageY 1 b function if g x X y Y if x 0 x 1 if y 0 y 1 else x x 9 3 y y 9 6 r with c r function font bold 140px sans serif textAlign center save clearRect 0 0 w h p i z 1 0 beginPath moveTo y y 2 x x 2 0 lineTo w 2 0 lineTo 0 h 2 lineTo 0 x x 2 y y 2 clip p o z 0 translate x y rotate Math atan2 y x 2 scale 1 1 p o z 1 restore p function s l b fillStyle s e f 0 fillText String fromCharCode l w 6 125 if b f 255 e f function s fillStyle rgba s s s 4 e function fillRect 0 0 w 3 h 3 v function o i for i 0 i 100 i i 10 Math random 4 5 i i 1 v v setInterval b 35 console log ready Smooth animated scroll effect let divx document createElement div document body appendChild divx divx style position absolute divx style border 1px solid red divx style borderRadius 8px divx style width 8px divx style height 8px divx style pointerEvents none let canvas document createElement canvas canvas width 720 canvas height 480 document body appendChild canvas n canvas with n c getContext 2d width w 720 height h 480 dragging X Y x y 0 d function a b c d return a c a c b d b d document onmousedown function e if d X Y x y w dragging 1 style cursor se resize refresh document onmouseup function e style cursor default dragging 0 if d w 3 h 3 x y 83200 x y 0 document onmousemove function e const rect canvas getBoundingClientRect X event clientX rect left Y event clientY rect top update function if dragging X w 0 06 Y h 0 1 x x X x 0 1 y y Y y 0 1 refresh with c refresh function font bold 140px sans serif textAlign center save clearRect 0 0 w h divx style left x 4 px divx style top y 4 px back f 0 folded page beginPath moveTo x y y x 0 5 0 similar triangles height rule https www onlinemathlearning com similar triangles circle html lineTo w 0 lineTo w h lineTo 0 h lineTo 0 x x y y 0 5 stroke Draw it clip f 1 front cover translate x y rotate Math atan2 y x 2 0 scale 1 1 f 2 restore f function s 0 back 1 front 2 backfolder if s 0 cc 90 if s 1 cc 100 if s 2 cc 200 fillStyle rgba cc cc cc 1 0 fillRect 0 0 w 2 h 2 if s 0 fillStyle ff0000 fillText B w 4 h 3 if s 1 fillStyle ff0000 fillText F w 4 h 3 if s 2 fillStyle ff0000 fillText M w 4 h 3 setInterval update 35 console log ready
of the left and right side shadows var paperShadowWidth PAGE_WIDTH 0 5 Math max Math min 1 flip progress 0 5 0 var rightShadowWidth PAGE_WIDTH 0 5 Math max Math min strength 0 5 0 var leftShadowWidth PAGE_WIDTH 0 5 Math max Math min strength 0 5 0 Change page element width to match the x position of the fold flip page style width Math max foldX 0 px context save context translate CANVAS_PADDING BOOK_WIDTH 2 PAGE_Y CANVAS_PADDING Draw a sharp shadow on the left side of the page context strokeStyle rgba 0 0 0 0 05 strength context lineWidth 30 strength context beginPath context moveTo foldX foldWidth verticalOutdent 0 5 context lineTo foldX foldWidth PAGE_HEIGHT verticalOutdent 0 5 context stroke Right side drop shadow var rightShadowGradient context createLinearGradient foldX 0 foldX rightShadowWidth 0 rightShadowGradient addColorStop 0 rgba 0 0 0 strength 0 2 rightShadowGradient addColorStop 0 8 rgba 0 0 0 0 0 context fillStyle rightShadowGradient context beginPath context moveTo foldX 0 context lineTo foldX rightShadowWidth 0 context lineTo foldX rightShadowWidth PAGE_HEIGHT context lineTo foldX PAGE_HEIGHT context fill Left side drop shadow var leftShadowGradient context createLinearGradient foldX foldWidth leftShadowWidth 0 foldX foldWidth 0 leftShadowGradient addColorStop 0 rgba 0 0 0 0 0 leftShadowGradient addColorStop 1 rgba 0 0 0 strength 0 15 context fillStyle leftShadowGradient context beginPath context moveTo foldX foldWidth leftShadowWidth 0 context lineTo foldX foldWidth 0 context lineTo foldX foldWidth PAGE_HEIGHT context lineTo foldX foldWidth leftShadowWidth PAGE_HEIGHT context fill Gradient applied to the folded paper highlights shadows var foldGradient context createLinearGradient foldX paperShadowWidth 0 foldX 0 foldGradient addColorStop 0 35 fafafa foldGradient addColorStop 0 73 eeeeee foldGradient addColorStop 0 9 fafafa foldGradient addColorStop 1 0 e2e2e2 context fillStyle foldGradient context strokeStyle rgba 0 0 0 0 06 context lineWidth 0 5 Draw the folded piece of paper context beginPath context moveTo foldX 0 context lineTo foldX PAGE_HEIGHT context quadraticCurveTo foldX PAGE_HEIGHT verticalOutdent 2 foldX foldWidth PAGE_HEIGHT verticalOutdent context lineTo foldX foldWidth verticalOutdent context quadraticCurveTo foldX verticalOutdent 2 foldX 0 context fill context stroke context restore console log ready script let canvas document createElement canvas canvas width 720 canvas height 480 document body appendChild canvas n canvas with n c getContext 2d width w 720 height h 480 g X Y x y i o 0 z 66 B d function a b c d return a c a c b d b d document onmousedown function e if d X Y x y w g 1 style cursor se resize r document onmouseup function e style cursor default g 0 if d w 3 h 3 x y 83200 x y 0 z v document onmousemove function e X e pageX 1 Y e pageY 1 b function if g x X y Y if x 0 x 1 if y 0 y 1 else x x 9 3 y y 9 6 r with c r function font bold 140px sans serif textAlign center save clearRect 0 0 w h p i z 1 0 beginPath moveTo y y 2 x x 2 0 lineTo w 2 0 lineTo 0 h 2 lineTo 0 x x 2 y y 2 clip p o z 0 translate x y rotate Math atan2 y x 2 scale 1 1 p o z 1 restore p function s l b fillStyle s e f 0 fillText String fromCharCode l w 6 125 if b f 255 e f function s fillStyle rgba s s s 4 e function fillRect 0 0 w 3 h 3 v function o i for i 0 i 100 i i 10 Math random 4 5 i i 1 v v setInterval b 35 console log ready Smooth animated scroll effect let divx document createElement div document body appendChild divx divx style position absolute divx style border 1px solid red divx style borderRadius 8px divx style width 8px divx style height 8px divx style pointerEvents none let canvas document createElement canvas canvas width 720 canvas height 480 document body appendChild canvas n canvas with n c getContext 2d width w 720 height h 480 dragging X Y x y 0 d function a b c d return a c a c b d b d document onmousedown function e if d X Y x y w dragging 1 style cursor se resize refresh document onmouseup function e style cursor default dragging 0 if d w 3 h 3 x y 83200 x y 0 document onmousemove function e const rect canvas getBoundingClientRect X event clientX rect left Y event clientY rect top update function if dragging X w 0 06 Y h 0 1 x x X x 0 1 y y Y y 0 1 refresh with c refresh function font bold 140px sans serif textAlign center save clearRect 0 0 w h divx style left x 4 px divx style top y 4 px back f 0 folded page beginPath moveTo x y y x 0 5 0 similar triangles height rule https www onlinemathlearning com similar triangles circle html lineTo w 0 lineTo w h lineTo 0 h lineTo 0 x x y y 0 5 stroke Draw it clip f 1 front cover translate x y rotate Math atan2 y x 2 0 scale 1 1 f 2 restore f function s 0 back 1 front 2 backfolder if s 0 cc 90 if s 1 cc 100 if s 2 cc 200 fillStyle rgba cc cc cc 1 0 fillRect 0 0 w 2 h 2 if s 0 fillStyle ff0000 fillText B w 4 h 3 if s 1 fillStyle ff0000 fillText F w 4 h 3 if s 2 fillStyle ff0000 fillText M w 4 h 3 setInterval update 35 console log ready