An warware: yadda ake canza zanen html zuwa fayil png

Babban matsalar da ke da alaฦ™a da canza zanen HTML zuwa fayil na PNG shine cewa zanen HTML ba ainihin hoto bane, sai dai wani abu mai ฦ™arfi. Wannan yana nufin cewa ba za a iya canza shi kai tsaye zuwa fayil ษ—in hoto kamar PNG ba tare da an fara canza shi zuwa tsarin hoto ba. Don yin wannan, dole ne a sanya zanen zuwa madaidaicin allo sannan a fitar dashi azaman fayil ษ—in hoto. Wannan tsari na iya zama mai rikitarwa kuma yana ษ—aukar lokaci dangane da rikitaccen zane da abin da ke cikinsa. Bugu da ฦ™ari, wasu fasalulluka na zane kamar inuwa ko gradients maiyuwa ba za su iya yin daidai lokacin da aka canza su zuwa tsarin hoto ba.

To convert a HTML canvas into a PNG file, you can use the <code>toDataURL()</code> method of the canvas element. This method returns a data URI containing a representation of the image in the format specified by the type parameter (defaults to PNG).

<canvas id="myCanvas" width="200" height="100"></canvas>
<script>
  var canvas = document.getElementById('myCanvas');
  var dataURL = canvas.toDataURL('image/png'); // defaults to png, can also use 'image/jpeg'
</script>

1. Layin farko ya ฦ™irฦ™iri sigar zane tare da id na "myCanvas" da faษ—i da tsayi na 200px da 100px bi da bi.
2. Layi na biyu ya haifar da wani canji mai suna "canvas" wanda ke adana abubuwan HTML tare da id na "myCanvas".
3. Layi na uku ya haifar da mabambanta mai suna "dataURL" wanda ke adana bayanan URI da aka dawo dasu toDataURL() Hanyar, ta amfani da 'image/png' azaman nau'in siga (tsoho zuwa PNG).

JPEG vs PNG tsarin fayil don gidan yanar gizon

JPEG da PNG sune nau'ikan fayilolin hoto da aka fi amfani dasu akan gidan yanar gizo. JPEG shine tsarin matsawa mai asara, ma'ana cewa wasu bayanan hoto suna ษ“acewa lokacin da aka matsa fayil ษ—in. Wannan ya sa ya dace don hotuna da sauran hadaddun hotuna inda za a iya sadaukar da wasu dalla-dalla don ฦ™aramin girman fayil. PNG, a gefe guda, tsarin matsawa ne mara asara, ma'ana cewa babu bayanan da ke ษ“acewa yayin matsawa. Wannan ya sa ya dace don hotuna tare da layi mai kaifi da rubutu, kamar tambura ko gumaka.

Fayilolin JPEG sun kasance sun fi ฦ™anฦ™anta fiye da fayilolin PNG, yana sa su sauri don saukewa da sauฦ™i don adanawa akan sabar yanar gizo. Duk da haka, ฦ™ila ba za su yi kama da kaifi ko bayyananne kamar takwarorinsu na PNG ba saboda asarar matsawa algorithm da aka yi amfani da su a cikin JPEGs.

Fayilolin PNG sun fi JPEGs girma amma suna ba da ingantattun hotuna masu inganci tare da ingantattun layuka da ingantattun launuka. Suna kuma goyan bayan fayyace wanda zai iya zama da amfani yayin ฦ™irฦ™irar tambura ko gumaka tare da bayyanannun tushe. Koyaya, suna iya ษ—aukar lokaci mai tsawo don saukewa saboda girman girman fayil ษ—in su idan aka kwatanta da JPEGs.

Menene zane

Canvas wani abu ne na HTML wanda za'a iya amfani dashi don zana zane-zane akan shafin yanar gizon ta amfani da harsunan rubutun kamar JavaScript. Ana amfani da shi don ฦ™irฦ™irar zane-zane, raye-raye, wasanni, da sauran abubuwan wadatar hoto. Kayan zane yana da hanyoyi da yawa don zana hanyoyi, kwalaye, da'ira, rubutu da ฦ™ara hotuna. Hakanan yana da halaye da yawa don saita faษ—i da tsayin wurin zane da sauran kaddarorin kamar launi na bango.

Yadda ake canza zane HTML zuwa PNG

Mayar da zane HTML zuwa hoton PNG tsari ne mai sauฦ™i.

1. ฦ˜irฦ™iri nau'in zane na HTML a cikin shafinku:

2. Zana wani abu akan zane:
var ctx = document.getElementById("myCanvas").getContext("2d");
ctx.fillStyle = "# FF0000";
ctx.fillRect (0,0,150,75);

3. Sami URL ษ—in bayanai na zane:
var dataURL = document.getElementById("myCanvas").toDataURL();

4. Maida URL ษ—in bayanan zuwa hoton PNG:
var img = sabon Hoto();
img.src = dataURL;

Shafi posts:

Leave a Comment