Jquery Draw Circle on Image

CanvasRenderingContext2D.drawImage()

The CanvasRenderingContext2D.drawImage() method of the Canvas 2D API provides unlike ways to draw an image onto the canvas.

Syntax

                                  void                  ctx.                  drawImage                  (paradigm,                  dx,                  dy)                  ;                  void                  ctx.                  drawImage                  (image,                  dx,                  dy,                  dWidth,                  dHeight)                  ;                  void                  ctx.                  drawImage                  (image,                  sx,                  sy,                  sWidth,                  sHeight,                  dx,                  dy,                  dWidth,                  dHeight)                  ;                              

drawImage

Parameters

image

An chemical element to depict into the context. The specification permits whatsoever canvas image source (CanvasImageSource), specifically, a CSSImageValue, an HTMLImageElement, an SVGImageElement, an HTMLVideoElement, an HTMLCanvasElement, an ImageBitmap, or an OffscreenCanvas.

sx Optional

The 10-centrality coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context. Utilize the 3- or 5-statement syntax to omit this argument.

sy Optional

The y-centrality coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context. Use the three- or 5-argument syntax to omit this statement.

sWidth Optional

The width of the sub-rectangle of the source image to depict into the destination context. If not specified, the unabridged rectangle from the coordinates specified by sx and sy to the bottom-right corner of the image is used. Apply the 3- or 5-argument syntax to omit this argument.

sHeight Optional

The height of the sub-rectangle of the source image to draw into the destination context. Apply the 3- or 5-argument syntax to omit this argument.

dx

The x-axis coordinate in the destination canvas at which to place the top-left corner of the source image.

dy

The y-axis coordinate in the destination canvas at which to place the top-left corner of the source paradigm.

dWidth

The width to describe the image in the destination canvas. This allows scaling of the drawn image. If not specified, the image is not scaled in width when drawn. Annotation that this argument is non included in the iii-argument syntax.

dHeight

The tiptop to describe the epitome in the destination canvass. This allows scaling of the drawn epitome. If non specified, the image is non scaled in summit when drawn. Note that this argument is not included in the 3-argument syntax.

Exceptions

InvalidStateError

Thrown if the image has no epitome information or if the sheet or source rectangle width or height is nil.

Examples

Drawing an image to the canvas

This example draws an image to the canvas using the drawImage() method.

HTML

                                                                            <sheet                    id                                          =                      "canvas"                                        >                                                                              </canvas                    >                                                                              <div                                          style                                              =                        "                                                  display                          :none;                                                "                                                              >                                                                              <img                    id                                          =                      "source"                                        src                                          =                      "rhino.jpg"                                        width                                          =                      "300"                                        superlative                                          =                      "227"                                        >                                                                              </div                    >                                                

JavaScript

The source epitome is taken from the coordinates (33, 71), with a width of 104 and a elevation of 124. It is drawn to the canvas at (21, xx), where it is given a width of 87 and a height of 104.

                                  const                  canvass                  =                  certificate.                  getElementById                  (                  'canvas'                  )                  ;                  const                  ctx                  =                  canvas.                  getContext                  (                  '2d'                  )                  ;                  const                  image                  =                  certificate.                  getElementById                  (                  'source'                  )                  ;                  image.                  addEventListener                  (                  'load'                  ,                  e                  =>                  {                  ctx.                  drawImage                  (image,                  33                  ,                  71                  ,                  104                  ,                  124                  ,                  21                  ,                  20                  ,                  87                  ,                  104                  )                  ;                  }                  )                  ;                              

Event

Understanding source element size

The drawImage() method uses the source element's intrinsic size in CSS pixels when drawing.

For case, if you load an Image and specify the optional size parameters in its constructor, you lot volition have to use the naturalWidth and naturalHeight properties of the created case to properly calculate things like crop and scale regions, rather than element.width and element.peak. The same goes for videoWidth and videoHeight if the element is a <video> element, and so on.

HTML

                                                                            <canvas                    id                                          =                      "canvas"                                        >                                                                              </canvas                    >                                                

JavaScript

                                  const                  canvas                  =                  document.                  getElementById                  (                  'canvass'                  )                  ;                  const                  ctx                  =                  sheet.                  getContext                  (                  '2d'                  )                  ;                  const                  image                  =                  new                  Image                  (                  lx                  ,                  45                  )                  ;                  // Using optional size for prototype                  image.onload                  =                  drawImageActualSize;                  // Draw when epitome has loaded                  // Load an image of intrinsic size 300x227 in CSS pixels                  image.src                  =                  'rhino.jpg'                  ;                  part                  drawImageActualSize                  (                  )                  {                  // Use the intrinsic size of image in CSS pixels for the canvas chemical element                  canvass.width                  =                  this                  .naturalWidth;                  canvas.top                  =                  this                  .naturalHeight;                  // Will draw the image as 300x227, ignoring the custom size of 60x45                  // given in the constructor                  ctx.                  drawImage                  (                  this                  ,                  0                  ,                  0                  )                  ;                  // To use the custom size nosotros'll have to specify the scale parameters                  // using the element's width and height properties - lets draw i                  // on height in the corner:                  ctx.                  drawImage                  (                  this                  ,                  0                  ,                  0                  ,                  this                  .width,                  this                  .height)                  ;                  }                              

Result

Specifications

Specification
HTML Standard
# dom-context-2d-drawimage-dev

Browser compatibility

BCD tables but load in the browser

Gecko-specific notes

  • Support for flipping the epitome by using negative values for sw and sh was added in Gecko v.0 (Firefox 5.0 / Thunderbird five.0 / SeaMonkey 2.2).
  • Starting with (Firefox 5.0 / Thunderbird five.0 / SeaMonkey 2.2), drawImage() handles negative arguments in accord with the specification, past flipping the rectangle around the appropriate axis.
  • Specifying a null or undefined prototype when calling or drawImage() correctly throws a TYPE_MISMATCH_ERR exception starting with (Firefox 5.0 / Thunderbird v.0 / SeaMonkey 2.ii).
  • Prior to Gecko seven.0 (Firefox vii.0 / Thunderbird vii.0 / SeaMonkey 2.4), Firefox threw an exception if any of the coordinate values was non-finite or cipher. As per the specification, this no longer happens.
  • Gecko 9.0 (Firefox nine.0 / Thunderbird ix.0 / SeaMonkey 2.6) now correctly supports CORS for drawing images beyond domains without tainting the canvas.
  • Gecko 11.0 (Firefox xi.0 / Thunderbird eleven.0 / SeaMonkey ii.8) now allows SVG-as-an-image to exist drawn into a canvas without tainting the canvas.

Notes

  • drawImage() but works correctly on an HTMLVideoElement when its HTMLMediaElement.readyState is greater than i (i.eastward., seek consequence fired later setting the currentTime property).
  • drawImage() will always utilize the source element's intrinsic size in CSS pixels when drawing, cropping, and/or scaling.
  • In some older browser versions, drawImage() will ignore all EXIF metadata in images, including the Orientation. This behavior is especially troublesome on iOS devices. You should detect the Orientation yourself and use rotate() to make it right.

See also

  • The interface defining this method: CanvasRenderingContext2D

carterdiefuld.blogspot.com

Source: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage

0 Response to "Jquery Draw Circle on Image"

إرسال تعليق

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel