canvas

Uncaught TypeError when calling drawImage function

心已入冬 提交于 2020-01-02 01:11:08
问题 Bonjour! I'm trying to make use of canvas element under ReactJS. I'm getting an error when i call drawImage(). Everything work except the drawImage().. ? Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(HTMLImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap)' var Canvas = React.createClass({ componentDidUpdate: function() { console.log("componentDidUpdate"); }, componentDidMount: function() { var context =

手把手教你图片转ASCII码图

橙三吉。 提交于 2020-01-01 23:50:53
效果图 基本思路 把图片每个像素点的信息拿出来,最重要的是拿到rgb的值 把每个像素点由rgb转成灰度图像,即0-255 给0-255分级,把每个等级的像素点转换成ascii码,完成 实现 第一步:获取像素信息 经查阅,使用canvas的getImageData方法可完成此要求,如下 <canvas id="canvas"></canvas> <script> var canvas=document.getElementById("canvas"); var context=canvas.getContext("2d"); canvas.width=800; canvas.height=800; context.rect(0,0,800,800); context.fillStyle="red"; context.fill(); console.log(context.getImageData(0,0,800,800)) </script> 上述代码指在canvas中铺满背景色为red,同时用getImageData()方法输出整个画布800*800的每个像素点。在控制台我们可以看到console的结果: 我们看到长度为2560000,而我们的宽*高才640000,这是怎么回事,难道不是一个像素点对应getImageData()中的一位?我们把2560000/640000

js 图片转换base64 base64转换为file对象

我的梦境 提交于 2020-01-01 23:46:58
function getImgToBase64(url,callback){//将图片转换为Base64 var canvas = document.createElement('canvas'), ctx = canvas.getContext('2d'), img = new Image; img.crossOrigin = 'Anonymous'; img.onload = function(){ canvas.height = img.height; canvas.width = img.width; ctx.drawImage(img,0,0); var dataURL = canvas.toDataURL('image/png'); callback(dataURL); canvas = null; }; img.src = url; } function dataURLtoFile(dataurl, filename) {//将base64转换为文件 var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); while(n--){ u8arr[n] = bstr

How to prevent black background when saving a div to png

社会主义新天地 提交于 2020-01-01 22:16:48
问题 I´m using HTML2canvas, filesaver.js and canvas2blob.js to achieve an in-browser save-dialogue. The on-the-fly canvas-creation and saving works fine except the image background is black. The problem is the base64-encoded image of the div with id="drop1" (the user drags and drops an image from his desktop to the html and then that image is put as background as base64). How can I achieve a visible output in the png file? my JS: // save img magic // html2canvas.js linked with filesaver.js and

Creating colorpicker on HTML5 canvas

南楼画角 提交于 2020-01-01 20:55:10
问题 How to draw a colorpicker on HTML5 canvas? 回答1: A basic example would be using getImageData : http://jsfiddle.net/eGjak/60/. var ctx = $('#cv').get(0).getContext('2d'); for(var i = 0; i < 30; i++) { for(var j = 0; j < 30; j++) { ctx.fillStyle = 'rgb(' + ((i/30*255)|0) + ',' + ((j/30*255)|0) + ',' + '0)'; ctx.fillRect(i * 10, j * 10, 10, 10); } } $('#cv').click(function(e) { // get pixel under mouse cursor var data = ctx.getImageData(e.offsetX, e.offsetY, 1, 1).data; alert('rgb: ' + [].slice

Why I get an Exception 18 when I try to get the image data of an Image with Access-Control-Allow-Origin: * in the response header?

China☆狼群 提交于 2020-01-01 19:12:32
问题 When I try to get the pixels of an image through the method getImageData, I've got this error "Unable to get image data from canvas because the canvas has been tainted by cross-origin data. Uncaught Error: SecurityError: DOM Exception 18" The image has Access-Control-Allow-Origin: * in the header response. So, I don't understand why I got this error. What I have to do to solve this problem?. I've tried to add the attribute crossOrigin to the image, but this does not work in Safari. The code I

Sketching with JS

别说谁变了你拦得住时间么 提交于 2020-01-01 18:51:22
问题 I am having a hard time thinking this one through Let's say you have a sketch app built with JS. For example: http://intridea.github.com/sketch.js/ And you want to store the user's drawing after they draw something. So that whenever someone visits the site it loads all the previous drawings. It's a bit faking realtime multiuser drawing, because making it really realtime is not an option at the moment. Server-side I am working with PHP and MySQL, I don't know if that's any good for this.

How to refresh a Canvas on a HTML bag?

痴心易碎 提交于 2020-01-01 18:16:13
问题 I have this javascript program that draws a hundred circles on the screen and they can bounce around in the canvas on themselves. Currently I draw an empty rectangle over them to erase next generation of them. But is there a better way to erase and refresh a Canvas on a HTML page. Code: function drawScreen () { context.fillStyle = '#EEEEEE'; context.fillRect(0, 0, theCanvas.width, theCanvas.height); //Box context.strokeStyle = '#000000'; context.strokeRect(1, 1, theCanvas.width-2, theCanvas

How to draw on canvas and convert to bitmap?

。_饼干妹妹 提交于 2020-01-01 17:00:49
问题 I'm tring to draw some line and shapes on canvas and then convert it to bitmap on ImageView. I'm usin a custom class that extands "View" and on "OnDraw method i'm drawing the lines. here is my code(this class only draw simple lines) : public class finalDraw extends View { public finalDraw(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint = new Paint(); paint.setColor(Color.BLUE); for (int i = 0; i <100; i++) { canvas

Change Color of Background in javaFX Canvas

倖福魔咒の 提交于 2020-01-01 14:53:27
问题 How do we change the background of JavaFX canvas? The only solution I have now is: GraphicsContext gc = canvas.getGraphicsContext2D(); gc.setFill(Color.BLUE); gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight()); Is there another solution except drawing a rectangle? I searched in CSS but canvas doesn't have -fx-background-color 回答1: The canvas is essentially "blank" (i.e. transparent) unless you draw onto it. You can create the effect of a background by placing it into a layout pane and