Advertisement
here2share

# js_putImageData2.py

Nov 28th, 2019
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.94 KB | None | 0 0
  1. # js_putImageData2.py
  2.  
  3. import tempfile
  4. import webbrowser
  5. import os
  6.  
  7. '''
  8. Colors, Styles, and Shadows
  9. Property                            Description
  10. ctx.fillStyle                       Sets or returns the color, gradient, or pattern used to fill the drawing
  11. ctx.strokeStyle                     Sets or returns the color, gradient, or pattern used for strokes
  12. ctx.shadowColor                     Sets or returns the color to use for shadows
  13. ctx.shadowBlur                      Sets or returns the blur level for shadows
  14. ctx.shadowOffsetX                   Sets or returns the horizontal distance of the shadow from the shape
  15. ctx.shadowOffsetY                   Sets or returns the vertical distance of the shadow from the shape
  16.  
  17. Method                              Description
  18. ctx.createLinearGradient()          Creates a linear gradient (to use on canvas content)
  19. ctx.createPattern()                 Repeats a specified element in the specified direction
  20. ctx.createRadialGradient()          Creates a radial/circular gradient (to use on canvas content)
  21. ctx.addColorStop()                  Specifies the colors and stop positions in a gradient object
  22.  
  23. Line Styles
  24. Property                            Description
  25. ctx.lineCap                         Sets or returns the style of the end caps for a line
  26. ctx.lineJoin                        Sets or returns the type of corner created, when two lines meet
  27. ctx.lineWidth                       Sets or returns the current line width
  28. ctx.miterLimit                      Sets or returns the maximum miter length
  29.  
  30. Rectangles
  31. Method                              Description
  32. ctx.rect()                          Creates a rectangle
  33. ctx.fillRect()                      Draws a "filled" rectangle
  34. ctx.strokeRect()                    Draws a rectangle (no fill)
  35. ctx.clearRect()                     Clears the specified pixels within a given rectangle
  36.  
  37. Paths
  38. Method                              Description
  39. ctx.fill()                          Fills the current drawing (path)
  40. ctx.stroke()                        Actually draws the path you have defined
  41. ctx.beginPath()                     Begins a path, or resets the current path
  42. ctx.moveTo()                        Moves the path to the specified point in the canvas, without creating a line
  43. ctx.closePath()                     Creates a path from the current point back to the starting point
  44. ctx.lineTo()                        Adds a new point and creates a line to that point from the last specified point in the canvas
  45. ctx.clip()                          Clips a region of any shape and size from the original canvas
  46. ctx.quadraticCurveTo()              Creates a quadratic Bezier curve
  47. ctx.bezierCurveTo()                 Creates a cubic Bezier curve
  48. ctx.arc()                           Creates an arc/curve (used to create circles, or parts of circles)
  49. ctx.arcTo()                         Creates an arc/curve between two tangents
  50. ctx.isPointInPath()                 Returns true if the specified point is in the current path, otherwise false
  51.  
  52. Transformations
  53. Method                              Description
  54. ctx.scale()                         Scales the current drawing bigger or smaller
  55. ctx.rotate()                        Rotates the current drawing
  56. ctx.translate()                     Remaps the (0,0) position on the canvas
  57. ctx.transform()                     Replaces the current transformation matrix for the drawing
  58. ctx.setTransform()                  Resets the current transform to the identity matrix. Then runs transform()
  59.  
  60. Text
  61. Property                            Description
  62. ctx.font                            Sets or returns the current font properties for text content
  63. ctx.textAlign                       Sets or returns the current alignment for text content
  64. ctx.textBaseline                    Sets or returns the current text baseline used when drawing text
  65.  
  66. Method                              Description
  67. ctx.fillText()                      Draws "filled" text on the canvas
  68. ctx.strokeText()                    Draws text on the canvas (no fill)
  69. ctx.measureText()                   Returns an object that contains the width of the specified text
  70.  
  71. Image Drawing
  72. Method                              Description
  73. ctx.drawImage()                     Draws an image, canvas, or video onto the canvas
  74.  
  75. Pixel Manipulation
  76. Property                            Description
  77. ctx.width                           Returns the width of an ImageData object
  78. ctx.height                          Returns the height of an ImageData object
  79. ctx.data                            Returns an object that contains image data of a specified ImageData object
  80.  
  81. Method                              Description
  82. ctx.createImageData()               Creates a new, blank ImageData object
  83. ctx.getImageData()                  Returns an ImageData object that copies the pixel data for the specified rectangle on a canvas
  84. ctx.putImageData()                  Puts the image data (from a specified ImageData object) back onto the canvas
  85.  
  86. Compositing
  87. Property                            Description
  88. ctx.globalAlpha                     Sets or returns the current alpha or transparency value of the drawing
  89. ctx.globalCompositeOperation        Sets or returns how a new image is drawn onto an existing image
  90.  
  91. Other
  92. Method                              Description
  93. ctx.save()                          Saves the state of the current context
  94. ctx.restore()                       Returns previously saved path state and attributes
  95. ctx.createEvent()    
  96. ctx.getContext()    
  97. ctx.toDataURL()
  98. '''
  99.  
  100. js_data = '''<!DOCTYPE html>
  101. <html>
  102.     <head>
  103.     <title>HTML5 putImageData 2</title>
  104.     </head>
  105.     <body>
  106.         <canvas id="myCanvas" width="1200" height="800"
  107.                         style="border:0px">
  108.         </canvas>
  109.         <script>
  110.             var canvas = document.getElementById('myCanvas'),
  111.                 ctx = canvas.getContext('2d');
  112.             var rectData = ctx.getImageData(10, 10, 500, 500);
  113.             function setpixel(x,y) {
  114.                 const offset = 4*(y*500+x);// 4* because each pixel is 4 bytes
  115.                 this.rectData.data[offset+0] = Math.floor(Math.random() * 256);// red
  116.                 this.rectData.data[offset+1] = Math.floor(Math.random() * 256);// green
  117.                 this.rectData.data[offset+2] = Math.floor(Math.random() * 256);// blue
  118.                 this.rectData.data[offset+3] = 255;// alpha, fully opaque
  119.             }
  120.             for (var y=0; y<500; y++) {
  121.                 for (var x=0; x<500; x++) {
  122.                     setpixel(x,y);
  123.                 }
  124.             }
  125.             ctx.putImageData(rectData, 10, 10);
  126.  
  127.             function animation() {
  128.                 for (var i=0; i<1000; i++) {
  129.                     var x = Math.floor(Math.random() * 500);
  130.                     var y = Math.floor(Math.random() * 500);
  131.                     setpixel(x,y);
  132.                 }
  133.                 ctx.putImageData(this.rectData, 10, 10);
  134.                 setInterval(animation, 0);
  135.             }
  136.             animation()
  137.  
  138.         </script>
  139.     </body>
  140. </html>
  141. '''
  142.  
  143. '''
  144.                 ctx.putImageData(rectData, 10, 10);
  145.  
  146. '''
  147.  
  148. chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
  149.  
  150. tf = tempfile.mktemp(".html", "JSdemo_")
  151. print tf
  152. with open(tf, 'w') as temp:
  153.     temp.write(js_data)
  154. webbrowser.get(chrome_path).open(tf)
  155. os.remove(tf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement