सैकड़ों ट्यूटोरियल हैं, कैनवास पर drawImage() द्वारा कोई छवि कैसे फसल कर सकती है।कुछ चौड़ाई और ऊंचाई के साथ क्रॉप कैनवास/निर्यात एचटीएमएल 5 कैनवास
context.drawImage(imageObj, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight);
हालांकि, मेरे पास एक कैनवास है जो उपयोगकर्ता के ब्राउज़र को भरता है। एक छवि के रूप में कैनवास निर्यात करके मैं केवल 640px * 480px का क्षेत्रफल (0 | 0) से निर्यात करना चाहता हूं।
समस्या: मैं toDataURL() के लिए केवल 640 * 480 कैनवास का उपयोग करने के लिए जावास्क्रिप्ट कैसे बता सकता हूं?
$("#submitGraphic").click(function(){
var canvas = document.getElementsByTagName("canvas");
// canvas context
var context = canvas[0].getContext("2d");
// get the current ImageData for the canvas
var data = context.getImageData(0, 0, canvas[0].width, canvas[0].height);
// store the current globalCompositeOperation
var compositeOperation = context.globalCompositeOperation;
// set to draw behind current content
context.globalCompositeOperation = "destination-over";
//set background color
context.fillStyle = "#FFFFFF";
// draw background/rectangle on entire canvas
context.fillRect(0,0,canvas[0].width,canvas[0].height);
// not working, seems to clear the canvas? browser hangs?
// seems that I can click a white image in the background
/*canvas[0].width = 640;
canvas[0].height = 480;*/
// not working either
/*canvas[0].style.width = '640px';
canvas[0].style.height = '480px';*/
// not working at all
/*context.canvas.width = 640;
context.canvas.height = 480;*/
// write on screen
var img = canvas[0].toDataURL("image/png");
document.write('<a href="'+img+'"><img src="'+img+'"/></a>');
})
पुनश्च::
यहाँ मैं अब तक किया है मैं आकार बदलने या पैमाने, बस कतरन/तय खिड़की को फसल के लिए नहीं करना चाहती। Here मैंने पढ़ा है कि आप केवल canvas.width और canvas.height निर्दिष्ट करते हैं - लेकिन यह कैनवास को साफ़ करता है।
बहुत बढ़िया काम करता है!/केवल एक चीज जो मुझे परेशान करती है वह यह है कि फ़ायरफ़ॉक्स पृष्ठ को लोड करने का संकेत देता है (टैब सर्कल मोड़ दिखाता है) जब छवि पहले से दिखाई दे रही है। ईएससी पर क्लिक करने से wyciwyg: // यूआरएल में दिखाता है !? ... "wyciwyg दस्तावेज.write परिणाम पृष्ठों का प्रतिनिधित्व करने के लिए उपयोग की जाने वाली एक आंतरिक यूआरआई योजना है"/लेकिन वैसे भी, खुश है कि आपने मुझे एक समाधान समाधान दिया है! –
"डेटा" वैरिएबल असाइन किया गया है लेकिन कभी भी उपयोग नहीं किया गया ... –
और न ही समग्र ऑपरेशन चर –