1. To avoid security errors, always serve your HTML (and scripts) from the same server as any images you're going to be working with. (Corollary: Don't "serve" your HTML and images from the local filesystem. That's a sure way to get security errors.) Install a local instance of Apache web server (or some other web server) and serve content to your browser from localhost, if need be.
2. If you're modifying pixels using context.getImageData( ), use putImageData( ) to draw back to the image, and be sure to supply all 3 arguments to putImageData( )! Here is a common pattern:
function doSomething() {
var canvasData =
context.getImageData(0, 0, imageObj.width, imageObj.height);
for (var x = 0; x < w; x++) {
for (var y = 0; y < h; y++) {
var idx = (x + y * w) * 4;
var r = canvasData.data[idx + 0];
var g = canvasData.data[idx + 1];
var b = canvasData.data[idx + 2];
// do something to r,g,b here
canvasData.data[idx + 0] = r;
canvasData.data[idx + 1] = g;
canvasData.data[idx + 2] = b;
}
}
// draw it back out to the screen:
context.putImageData(canvasData, 0, 0);
}
Notice the three arguments to putImageData(). The final two args are the x and y position at which to draw the image. If you forget those two args, expect errors.
3. You can draw offscreen by simply creating a canvas element programmatically. Like this:
imageObj = new Image();
imageObj.src = "http://localhost:4502/content/lena.png";
function getOffscreenContext(imageObj) {
var offscreenCanvas = document.createElement("canvas");
offscreenCanvas.width = imageObj.width;
offscreenCanvas.height = imageObj.height;
return offscreenCanvas.getContext("2d");
}
If you use this function (or one like it), you can keep an offscreen copy of your image around, which can be extremely handy.
4. You can save programmatically created/modified images offline. The trick is to slurp the canvas into a data URL and then open or display that URL in a new frame or window where you can right-click it to get the usual image-save options from the browser. Something like this:
myImage = canvas.toDataURL("image/png");
window.open( myImage ); // opens in new window as a PNG
This serializes the image as a (big, huge) data URL, then opens the image in a new window. The new window contains a PNG image, plain and simple.
5. Any time you assign a value to canvas.width or canvas.height, you will wipe the canvas clean! This is both weird and handy. Just doing canvas.width = canvas.width will instantly erase the canvas.
6. When all else fails, consult the HTML 5 Canvas Cheatsheet.
inspirational quotes for drug addicts
ReplyDeleteovercoming addiction quotes
ReplyDeleteشركة كيان لنقل العفش بالرياض والمدينة المنورة وجدة ومكة والطائف والدمام تقديم لكم دليل كامل لشركات نقل العفش بالمملكة العربية السعودية
نقل عفش شركة كيان
دليل شركات نقل العفش
شركة نقل عفش بالمدينة المنورة
شركة نقل اثاث بالرياض
شركة نقل عفش بجدة
شركة نقل عفش بمكة
شركة نقل عفش بالطائف
ReplyDeleteشركة نقل عفش
اهم شركات مكافحة حشرات بالخبر كذلك معرض اهم شركة مكافحة حشرات بالدمام والخبر والجبيل والخبر والاحساء والقطيف كذلك شركة رش حشرات بالدمام ومكافحة الحشرات بالخبر
شركة مكافحة حشرات بالدمام
شركة تنظيف خزانات بجدة الجوهرة من افضل شركات تنظيف الخزانات بجدة حيث ان تنظيف خزانات بجدة يحتاج الى مهارة فى كيفية غسيل وتنظيف الخزانات الكبيرة والصغيرة بجدة على ايدى متخصصين فى تنظيف الخزانات بجدة
شركة تنظيف خزانات بجدة
شركة كشف تسربات المياه بالدمام
شركة نقل عفش واثاث
ReplyDeleteشركة سكاي لخدمات نقل العفش والاثاث بالمنطقة العربية السعودية نحن نوفر خدمات نقل اثاث بالرياض ونقل عفش بالمدينة المنورة ونقل عفش بمكة ونقل عفش بالطائف نحن نقدم افضل نقل اثاث بخميس مشيط ونقل عفش بجدة
شركة سكاي نقل العفش
مدونة لنقل العفش
شركة نقل عفش بمكة
شركة نقل عفش بالرياض
شركة نقل عفش بالمدينة المنورة
شركة نقل عفش بجدة
شركة نقل عفش بالطائف
شركة نقل عفش بالدمام
I appreciate your hard work. Keep posting new updates with us.
ReplyDeleteAssignment Writing Service
Thank you for sharing such an educative article. I am a freelancer at Rewrite my Essay website.
ReplyDelete