w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Difference between drawing directly to canvas and drawing on bitmap and then canvas in Android onDraw()
I disagree that you are saving much by going the bitmap route. You might consider a data structure that stores the drawn signal and converting this to a JSON object that can be serialized/deserialized. As for your question, this is an educated guess but it has more to do with rescaling of the drawn signal, since you are not using getHeight() when you create the bitmap.

Categories : Android

Drawing a Canvas into a smaller Canvas not working
Canvas objects don't like to be resised. After drawing Your image simply convert it toDataURL() and set as image source. They You may resize image as you want. $img.attr('src',canvas.toDataURL());

Categories : Javascript

get dataurl of canvas after each new drawing in canvas
Put the .push() inside your onload: dataUrls.push(canvas[0].toDataURL("image/jpeg")); Remember that map_bg_img.src is asynchronous so it will start working and immediately release to the next line in your code--so your looping is going faster than you expect.

Categories : Javascript

Drawing using canvas
Yes, each View has its own Canvas. You can convert drawable resources to Bitmaps using: BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_resource);

Categories : Android

Drawing on a canvas in OnTouchListener
It seems, that the bitmap you draw on is not the same size as the ImageView. So you paint on the bitmap using the event coordinates and when the bitmap is displayed, it's scaled to a different size due to the scaleType="fitCenter" attribute.

Categories : Android

Drawing in canvas does not center
Making your canvas be 100 pixels wide via CSS does not affect the conceptual width of the canvas for drawing purposes. To set that you can use the "width" and "height" properties on the canvas tag. <canvas width=100 height=100></canvas> If you don't specify that, the default is 300 wide by 150 high.

Categories : Javascript

Android: Drawing on a new canvas
I figured out what was wrong with my understanding. It all seems so simple now! This link helped. Basically, when we create a Canvas object, it's creating an offscreen canvas that holds our drawing calls. This Canvas object needs to be backed by a mutable Bitmap, so that all the drawings finally get stored onto this Bitmap. Once the drawing is done, you can draw the mutable bitmap using the canvas that is tied to the surface (the canvas that you get in onDraw). In my code, I made the following changes to get it working. protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawCircle(100, 100, 50, paint); canvas2.drawCircle(25, 25, 25, paint); canvas.drawBitmap(backingBitmap, 200, 90, paint); }

Categories : Android

Drawing "holes" in a canvas
There are questions which may be revealnt for your situation : draw path with hole ( android ) Android canvas - Draw a hole Punch a hole in a rectangle overlay with HW acceleration enabled on View http://www.mail-archive.com/android-developers@googlegroups.com/msg232764.html

Categories : Android

Drawing a rectangle on Canvas
You need to modify a couple of things in the code: (edit: there are many issues with this code. I went through some of them inline here, but haven't tested. If you put it in a fiddle it's easier for us to check).. UPDATE: Working fiddle: http://jsfiddle.net/AbdiasSoftware/kqW4X/ When mouse down occur initialize both start and end points. Call a common draw function that is not dependent on the event itself: function mouseDown(eve) { mouseIsDown = 1; var pos = getMousePos(canvas, eve); startX = endX = pos.x; startY = endY = pos.y; drawSquare(); //update } At mouse up, only register if isMouseDown is true, else this function will handle all incoming up-events (as you have attatched it to document, which is correct - window could have been used too): function mouseUp

Categories : Javascript

Drawing Canvas on JFrame
After painting on canvas try repaint the JFrame public static void main(String[] args) { Game ga = new Game(); JFrame frame = new JFrame (); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.add(ga); frame.setVisible(true); ga.start(); //repaint here frame.repaint(); } Also note that: frame.pack for fixing the size issue. frame.revalidate sometimes help when adding or removing components in runtime.

Categories : Java

Drawing on Canvas and refreshing
Why do you set needToPaintShip = false; after calling invalidate()? Don't you need to draw the ship again in subsequent frames? Also, it seems like this item: Rect r = new Rect(myX*(rebro_piece),myY*rebro_piece, myX*(rebro_piece+1), myY*(rebro_piece+1)); should probably be: Rect r = new Rect(myX*(rebro_piece),myY*rebro_piece, (myX+1)*rebro_piece, (myY+1)*rebro_piece)); As for why the ship always appears in the bottom right corner, that depends on what you pass to drawShip(x,y), which isn't shown. Is it possible that you are passing pixel coordinates instead of something in the range [0-10)?

Categories : Java

Drawing to canvas like in paint
EDIT: Sorry, fixed some typos Edit 2: And again. It's a bit difficult to test. There's no reason to redraw each of the points each time. You could modify your listener to do this: var prevMouseX=null,prevMouseY=null; $('#canvasCursor').mousedown(function (e) { paint = true; console.log('down'); //get current mouse coords mouseX = e.pageX - this.offsetLeft; mouseY = e.pageY - this.offsetTop; if (prevMouseX==null) { //store these coordinates for next time if they haven't been defined yet prevMouseX = mouseX; prevMouseY = mouseY; } }); $('#canvasCursor').mousemove(function (e) { //get current mouse coords mouseX = e.pageX - this.offsetLeft; mouseY = e.pageY - this.offsetTop; if (prevMouseX==null) { //store the

Categories : Javascript

drawing a div element inside a canvas
You can't access an image stored as a CSS background image like that - there is no image element. It does work if the image is a real <img> tag within the <div> See http://jsfiddle.net/alnitak/DuCVG/1/ The (much harder) alternative would be to use window.getComputedStyle() to read the background-image CSS property from the element, parse it, and then create a brand new Image object that has the same source.

Categories : Javascript

HTML5 Canvas blinking on drawing
Right now you're reloading the image every frame and unless the onload callback fires within the 16ms of the frame you're going to see a blank canvas. You should only need to call the new Image, img.onload sequence once, to preload your images. The onload callback would then kick off your first frame, and the draw calls are free to use the image in memory. Something like: var camerax = 300, cameray = 100; var fps = 60; var img; var loop; function init() { img = new Image(); img.onload = function() { loop = setInterval(function() { update(); draw(); }, 1000/fps); }; img.src = "ground.png"; } function draw() { clearCanvas(); drawGround(); } function drawGround() { var width = img.width; var height = img.height;

Categories : Javascript

Efficient Drawing with canvas HTML5
Use layers and only redraw parts of the canvas that have actually changed. Here's a decent introduction to doing it by hand: http://www.ibm.com/developerworks/library/wa-canvashtml5layering/ Alternatively, use a library which has a nice abstraction around these concepts: http://www.createjs.com/#!/EaselJS

Categories : HTML

Canvas image drawing order
Your intuition is correct...you need an image loader. There are plenty of image loader patterns out there--here's one: When all your images are fully loaded, the imgs[] array below has your sprites in proper order. var imgURLs = ['1', '1000','1010','1011','1012','1013']; var imgs=[]; var imgCount=0; function pre_load(){ for(var i=0;i<imgURLs.length;i++){ var img=new Image(); imgs.push(img); img.onload=function(){ if(++imgCount>=imgs.length){ // imgs[] now contains all your images in imgURLs[] order render(); } } img.src= "img/sprites/"+imgURLs[i]+".png"; } }

Categories : Javascript

canvas drawing slow in firefox
I think you're doing a lot of computations and drawing for something that could be obtained in a much simpler way. In other words it's not Firefox that is slow... it's just that Chrome is blazing fast :-D An alternative approach would be for example having a partially transparent image displayed by the browser on top of a canvas and then your drawing operations are performed on the canvas directly without special masking. What this could allow is seeing the painting through the mask without having to do complex clipping manipulations. Those operations could be done for real on a single canvas only when the user asks to export the picture as a png if this is something you need to provide. To see an example of this approach in action check this out The source code is in lisp but it sho

Categories : HTML

Canvas: returning to state before drawing
if you want to undo drawImage changes, you must save canvas data before doing drawImage like: tmp = canvas.getContext("2d").getImageData(0, 0, with, height); //do changes here canvas.getContext("2d").putImageData(tmp , 0, 0); //here changes will be lost this is unswer to the title, question content was not clear for me

Categories : Javascript

Drawing line in Canvas having issue
TO CLEAR This is the code that you have to change to have a reusable code: c.moveTo(a/(can.clientWidth/can.width), b/(can.clientHeight/can.height)); a = event.clientX - parentPosition.x; b = event.clientY - parentPosition.y; console.log('a:'+a+'--b:'+b); c.lineTo(a/(can.clientWidth/can.width), b/(can.clientHeight/can.height)); If you don't use this, the code below works only for 300x300 canvas size. LAST EDIT: By analyzing the 2d context (var c = can.getContext('2d');console.log(c);) you can easily understand this error: there is a relation (not explicit) from the canvas and it's 2d context. As you can see from the image: the height and the width of the canvas(this is 500*500) are differents from its offset,scroll and client size. So, before to

Categories : Javascript

Drawing more than 1 to canvas wont work?
for(var i = 0; i < enemies.length; i++) { enemies[1].draw(); } should probably be for(var i = 0; i < enemies.length; i++) { enemies[i].draw(); } ...or you'll draw the same enemy over and over.

Categories : Javascript

Drawing img containing SVG source with canvas to get Data URL
You need to set the dimension of the canvas to the dimensions of the image. Here's an update to your fiddle. var canvas = document.createElement('canvas'); canvas.width = image.width; canvas.height = image.height; image.onload = function() { var canvas = document.createElement('canvas'); canvas.width = image.width; canvas.height = image.height; var context = canvas.getContext('2d'); context.drawImage(image, 0, 0); image.src = canvas.toDataURL(); } Adding image.onload makes this work in Firefox.

Categories : Javascript

Drawing Image in Canvas using DataURL
youre missing the onload event of the image: var image = new Image image.src = "URL or DataURL" image.onload = function(){ ctx.drawImage(image) } working fiddle

Categories : Javascript

Android: Drawing to Canvas While in Loop
I believe your issue is in your set loop: for(int i = 0; i <= 30; i++) { point1.set(i, i); pointList.add(i, point1); } In java, all variables containing objects are actually pointers. Thus point1 always points to the same object, since you never change what it is pointing at. On each loop iteration, you change the pointed to object, and re-insert it into the list. Thus pointList contains 31 instances of the same point, all at (30, 30). Instead, you should create a new point on each iteration, so your code looks like: for(int i = 0; i <= 30; i++) { Point p = new Point(); p.set(i, i); pointList.add(i, p); } Which will then create a new point on each iteration of the loop. You also no longer need a point1 variable. Also, you have your points very close together. Y

Categories : Android

Drawing a Picture to a View's Canvas does nothing
try this in your Activity: View view = new View(this); Picture picture = new Picture(); Canvas c = picture.beginRecording(100, 100); Paint p = new Paint(); c.drawColor(Color.WHITE); p.setColor(Color.RED); c.drawCircle(50, 50, 40, p); picture.endRecording(); Drawable d = new PictureDrawable(picture); view.setBackgroundDrawable(d); setContentView(view); you should see small red circle on the white background (tested on 2.3 emulator)

Categories : Android

Optimize Bulk drawing using Canvas
If you're really that worried about it, you could just have two Rect objects and use public void set (int left, int top, int right, int bottom) to change the values, and have them as member variables in the class to keep the GC from deleting them.

Categories : Java

Randomize drawing of canvas sections
How to efficiently spawn random sprites from a spritesheet Store the position and dimensions of each sprite on a spritesheet in an object: {x:50, y:15, width:170, height:200} Then push all sprite definition objects into an array // store x/y/width/height of each sprite in an object // add each object to the sprites array var sprites=[] sprites.push({x:50,y:15,width:170,height:200}); sprites.push({x:265,y:10,width:140,height:200}); sprites.push({x:460,y:10,width:180,height:200}); sprites.push({x:10,y:385,width:180,height:180}); sprites.push({x:225,y:395,width:200,height:200}); sprites.push({x:445,y:305,width:200,height:160}); Then when you want a random sprite, just use Math.random to pull one out of the sprites array: function spawnRandomSprite(){ // choose a random sprite an

Categories : Javascript

drawing a bunch of images on the canvas
Are you updating x, y, position, and height? If not, you may be drawing over the images, which would make it look like some aren't showing up. However, you should post more info, such as how many images are showing up, the whole function you are using to draw the images with, and a screenshot of what your attempt looks like. That way, we can help you more effectively

Categories : Javascript

Android: Is this not how drawing a canvas works?
You can set margins to move view in layout. I've created test project to show the idea, it looks like that: public class DragView extends View { private float mLastTouchX; private float mLastTouchY; private float mDeltaX; private float mDeltaY; public DragView(Context context) { super(context); init(); } public DragView(final Context context, final AttributeSet attrs) { super(context, attrs); init(); } private void init() { setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { final int action = event.getAction(); mLastTouchX = event.getRawX(); mLastTouchY = event.getRawY();

Categories : Android

Canvas not drawing image from spritesheet
ctxEnemy.drawImage(spriteImage,this.srcX,this.srcY,this.playerWidth,this.playerHeight,this. drawX,this.drawY,this.playerWidth,this.playerHeight); Your Enemy object don't have playerWidth and playerHeight properties. You really may want to model them with the same constructors to avoid such confusions.

Categories : Javascript

How can I interpolate between 2 points when drawing with canvas?
You want to draw a line from last mouse position to the new mouse postition. But you are drawing a circle at the new position instead. Why? Why don't you do it this way? onTouchStart: ctx.moveTo(x,y); onTouchMove: ctx.lineTo(x,y); All interpolation, antialiasing etc. is already included in "lineTo" function. Use ctx.lineCap = "round"; to have round corners. If you still want to interpolate numbers, here is the function: function interpolate(a, b, frac) // points A and B, frac between 0 and 1 { var nx = a.x+(b.x-a.x)*frac; var ny = a.y+(b.y-a.y)*frac; return {x:nx, y:ny}; } interpolate({x:2, y:2}, {x:4,y:4}, 0.5); // returns {x:3, y:3}

Categories : Javascript

Trouble with Drawing a Bitmap on a Canvas
You should never pull a bitmap out of resources in onDraw because onDraw is called numerous times per second, so there is no way it will have time to decode that resource every single time onDraw is called. You should decode your bitmap in your constructor, save it off in a class variable and use in in onDraw. So, basically, all you need to do is this: //add bitmap to class variable private Bitmap bitmap; //move these to constructor int resourceId = getResources().getIdentifier("bar1", "drawable", packageName); bitmap = BitmapFactory.decodeResource(getResources(), resourceId); And everything should work.

Categories : Android

html 5 canvas drawing error
Two thoughts: First: Wow, nice use of paths to lay out a map--very extensive! Second: Since you're driving your zooming with scrollwheel/touch rather than mouseclicks, you need to remove the requestAnimFrame animation. The animation was meant to gradually zoom to the point where the user mouseclicked.

Categories : Javascript

Drawing text on a scaled Canvas
When hardware acceleration is on, text is rendered into a texture at the font size you specify on the paint. This means that in your case, text is rasterized at 0.2f pixels. That texture is then scaled up at drawing time, which is why you're not seeing anything. While this issue has been addressed in a future version of Android, I would strongly recommend you didn't use 0..1 values. You might not only run into precision issues but you will also force the rendering pipelines (software and hardware) to bypass very useful optimizations. For instance, text rendered with a scale transform in software is rendered using paths instead of bitmaps blits.

Categories : Android

Canvas not drawing correctly (unstable)
Loading (Creating) an Image is an asynchronous operation. In your code you are doing something like this (pseudo code): var img = new Image(); img.src = "http://example.com/myImage.png"; canvas.drawImage(img, ...); If you have a fast connection (local LAN, ...) or the image is cached then maybe your image after "img.src=..." is ready to use for drawing. After pressing F5 several times, I guess your images is in the cache so it seams to work. To solve your problem, you have to wait for the loaded event. var img = new Image(); img.onload = function() { /* insert draw code here */ } img.src = "http://example.com/myImage.png"; For a game development, I recommend to build or use a asset library, that deals with loading/caching of image resources. By the way, turn off browser caching (

Categories : Javascript

Expanding the drawing on canvas for android
Well so I figured out the solution to it. What we can do is that we can use canvas.scale( newX, newY, pivotX, pivotY); /* * Where * newX = oldX * (newWidth/oldWidth); * newY = oldY * (newHeight/oldHeight); * pivotX = same as before; * pivotY = same as before; */

Categories : Android

drawing image on html5 canvas
Edited because I'm a noob (lucky cache hit): <script> var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); var marthPic = new Image(); marthPic.src = 'images/marth.jpg'; marthPic.onload = function() { ctx.drawImage(marthPic,10,10); }; </script>

Categories : HTML

drawing to canvas on android motion events
As Raghunandan sais, do not try to draw in any method other than onDraw(). Set your X and Y values in onTouch() and then invalidate the view. This will call onDraw() again.

Categories : Android

WPF C# Canvas Drawing Line connecting buttons
Whilst you can do it by moving the Canvas around, you might be better off using the positions of the Buttons themselves on the Canvas. Essentially, you need to have two sets of coordinates, your LineStart and LineEnd, which in this case will be obtained from the two different Buttons you're clicking. You could use Point location = ((Button)source).TransformToAncestor(Canvas1).Transform(new Point(0, 0)) to obtain the location of the button being clicked, relative to the parent Canvas. You would then need to do a similar thing to find the location of the original object that you had clicked (which you don't seem to be storing). You could then simply draw a line between the two calculated Points by setting the X1, X2, Y1, Y2 values. Adapting your example, you could do something like this

Categories : C#

Incorrect mouse coordinates when drawing on Canvas
A canvas has two distinct sizes: The size on the page The size in pixel of the image You need to set the canvas size in pixel to the size on the page to get an accurate 1:1 rendering: canvas.width = canvas.offsetWidth; canvas.height = canvas.offsetHeight; Please remember that when you set the width/height members of a canvas object the picture is cleared (even if you are setting the same value currently present).

Categories : Javascript

Html5 canvas pattern drawing delay
EDITED Sorry, I completely misunderstood your question. I just now went back and saw the last question you posted and the goal you are trying to achieve. To get the functionality you desire you can just create one function, you don't need two. Instead of using a second canvas in the HTML I created a temporary one using javascript. Here is the simplified and functional code <canvas id="sleeve" width='436' height='567'></canvas> var sleeve = document.getElementById('sleeve'); var ctx = sleeve.getContext('2d'); function rotator2(var2, var3) { // Draw the original sleeves var imageObj_rotator2 = new Image(); imageObj_rotator2.src = var2; imageObj_rotator2.onload = function () { var imgsleeve = new Image(); imgsleeve.src="http://i.stack.imgur.c

Categories : Javascript



© Copyright 2017 w3hello.com Publishing Limited. All rights reserved.