Using drawImage to draw a filled rectangle - drawimage

if I want to draw a filled rectangle, I could do
g.fillRect(0, 0, 100, 100)
But if I want to draw the same filled rectangle with drawImage, what do I do? I tried the below:
g.drawImage(null, 0,0,100, 100, Color.BLACK, null);
And it does not work. I am not going to use an image, but must the Image still be non-null? What would be the correct way to do this?

Related

jspdf: how to clip region/shapes?

I am trying to draw some shapes and lines in a restricted area.
http://raw.githack.com/MrRio/jsPDF/master/docs/jsPDF.html#clip
The docs says there is a clip() method, but how can I achieve this exactly? And after I have clipped to shapes, is it possible to draw again outside of the clipping region? (so back to normal)
A clear example would be appreciated. Thank you.
I am using jspdf in combo with jspdf-autotable.
After some trial and error, I have found a solution:
pdf.saveGraphicsState(); // pdf.internal.write('q');
// draw clipping objects
pdf.rect(0, 0, 100, 100, null); // important: style parameter is null!
pdf.clip();
// draw objects that need to be clipped
pdf.setFillColor('#f54f44');
pdf.rect(0, 0, 100, 100, 'F');
pdf.setFillColor('#54ff54');
pdf.rect(50, 50, 100, 100, 'F');
// restores the state to where there was no clipping
pdf.restoreGraphicsState(); // pdf.internal.write('Q');
// these objects will not be clipped
pdf.setFillColor('#001100');
pdf.rect(80, 80, 100, 100, 'F');

Any way to make an image round in Konvajs?

I have a Konva image. How do I set radius or border-radius?
In the docs the Image class has width and height, but I want to set the Radius (border-radius). There is a circle class that can have an image as a background. However, when I use this you need to specify the dimensions for each image to make it zoom into and crop the correct location.
<v-image
:config="{
x: 50,
y: 50,
image: image,
shadowBlur: 5
}"
/>
In the Image class there should be a property Radius. Just like in the circle class. Is their an alternative way to do this that I am missing?
If you want rounded corners, or even a complete circle, you need to use a clipping function applied to your group or shape.
See Konva docs example here
The important part if that in the creation of your group or shape you include the definition of the clip func. The example below creates 2 overlapping circles - see the Konva docs for the working code.
var group = new Konva.Group({
clipFunc: function(ctx) {
ctx.arc(250, 120, 50, 0, Math.PI * 2, false);
ctx.arc(150, 120, 60, 0, Math.PI * 2, false);
}
});
For rounded corners see the answer to a similar question with a working code snippet here.

UIImage drawInRect

I want to "extract" a apart of a UIImage but Im having some troubles.
For example the original imagesize is 320x480 and I want to get render a new UIImage in a rect like CGRect(10, 10, 100, 100)
Ive not found a good solution but Ive found something that might be close to the right solution: drawInrect().
But when I use that everything else but within that rect gets black.
Please help me.
If what you are trying to achieve is a cropped version of the original image, then you should know that the canvas you are drawing in as frame with {0,0} origin. So for example if your desired effect is to get an image with size 100x100 and origin {10, 10}. So ..
let imageSize = image.size
UIGraphicsBeginImageContextWithOptions(CGSizeMake(100.0, 100.0), true, 0.0)
image.drawInRect(CGRectMake(-10, -10, imageSize.width, imageSize.height))

change the color of images using renderTarget

I try to draw an image on the screen with render target.
I used this code:
_renderTarget = new RenderTarget2D(
this._graphicsDevice,
this._graphicsDevice.PresentationParameters.BackBufferWidth,
this._graphicsDevice.PresentationParameters.BackBufferHeight,
false,
this._graphicsDevice.PresentationParameters.BackBufferFormat,
DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
_graphicsDevice.SetRenderTarget(_renderTarget);
_spriteBatch.Begin();
_spriteBatch.Draw(texture, drawPoint, null, Color.Red, 0.0f
, new Vector2(texture.Width / 2, texture.Height / 2), 0.5f, SpriteEffects.None, 0 .0f);
_spriteBatch.End();
_graphicsDevice.SetRenderTarget(null);
But, the result image is always black!
Could you help me to change the color of this image.
Thanks.
From the code shown, _spriteBatch.Draw is only rendering content to _renderTarget.
Next you need to render the resulting RenderTarget2D to your screen so you can see it.
You already have _graphicsDevice.SetRenderTarget(null) in place. You then just need to make a separate SpriteBatch.Draw call passing in your _renderTarget.
You can do this because RenderTarget2D extends Texture2D.

How to straighten line endings in Cocos2d?

I'm new to Cocos2d and am trying out some of the basic drawing functions. When I draw a straight line with a high width (50 in this case), the ends of the line are not what I'd expect. What I'd like is for the line to be the same as it would be if I were using CoreGraphics, like this:
however what I see in Cocos2d is this:
The code I'm using to draw the line is in the layer's draw method:
-(void)draw
{
glColor4f(1, 0, 0, 1);
glLineWidth(50);
ccDrawLine(ccp(50, 50), ccp(250, 250));
}
Can anyone tell me how I can get cocos2d to draw a line with the same shape as the green image, rather than the red image?
Try drawing it antialiased.
glColor4f(1, 0, 0, 1);
glLineWidth(50);
glEnable(GL_LINE_SMOOTH);
ccDrawLine(ccp(50, 50), ccp(250, 250));

Resources