Openlayers rotation broken when using precompose to clip a layer - openlayers-3

After updating Openlayers to >4.0 map rotation is completely broken when using a precompose hook:
function precompose(event) {
var context = event.context;
context.beginPath();
context.moveTo(0, 0);
context.lineTo(100, 0);
context.lineTo(100, 100);
context.lineTo(0, 100);
context.lineTo(0, 0);
context.closePath();
context.fillStyle = "rgba(0, 5, 25, 0.75)";
context.fill();
}

It is important to save the canvas context, for more information see MDN CanvasRenderingContext2d
function precompose(event) {
var context = event.context;
context.save(); // be sure to save the context before anything is done
// ...
}
Also, use context.restore() in the postcompose hook.

Related

p5.js image in a buffer

I am trying to load image "image.png" and place it in the preaviously created createGraphics() buffer.
However when I load the buffer in draw() the image is not there.
I am wondering if an image can be loaded into a buffer, and if so how ?
var buffer;
var image;
function setup() {
createCanvas(800, 800);
image = loadImage("image.png");
buffer = createGraphics(800, 800);
buffer.background(255, 255, 0);
buffer.image(image, 0, 0)
}
function draw() {
image(buffer, 0, 0);
}
any help would be greatly appreciated.
It is not good to call your variable image as p5.js already has an image() function (which you use).
Use preload() to load the image. This ensures that the image is actually loaded before you try to load it in your buffer within setup(). See here for more info on this.
A working example is given here. It uses the following p5 code.
var buffer;
var img;
function preload() {
img = loadImage("p5im.png");
}
function setup() {
createCanvas(600, 600);
buffer = createGraphics(400, 400);
buffer.background(255, 255, 0);
buffer.image(img, 0, 0)
}
function draw() {
image(buffer, 0, 0);
}

Custom Hit Detection on Konva Group

I'm attempting to expand the click area of a group of shapes, but there appears to be no hitFunc property on groups.
var patternControl = new Konva.Group();
patternControl.hitFunc(function(context) {
context.beginPath();
context.arc(0, 0, outerRadius + patternWidth, 0, Math.PI * 2, true);
context.fillStrokeShape(this);
});
Is there any way to do apply custom hit functions to a group?
Only shapes can be used for hit detection. As a workaround, you can disable hits for all shapes with shape.listeting(false) and then add a "fake" shape to the group that will be used as a hit area:
var patternControl = new Konva.Group();
var hitShape = new Konva.Shape({
// make it transparent, so it is not visible
fill: 'rgba(0,0,0,0)',
hitFunc: (context, shape) => {
context.beginPath();
context.arc(0, 0, outerRadius + patternWidth, 0, Math.PI * 2, true);
context.fillStrokeShape(shape);
}
});
patternControl.add(hitShape);

How to draw bezier curve randomly for every second? also using the transition.dart

I want to know how to draw bezier curve randomly that change every second with animation.
That's a very specific question. To generic answer is that each draw command on the graphics class returns an object. You can use this object to change the properties (x, y, color, width, ...) of the draw command later. This example should give you an idea:
import 'dart:html' as html;
import 'package:stagexl/stagexl.dart';
void main() {
var canvas = html.querySelector('#stage');
var stage = new Stage(canvas, width: 800, height: 600);
var renderLoop = new RenderLoop();
renderLoop.addStage(stage);
var shape = new Shape();
var movetoCommand = shape.graphics.moveTo(100, 100);
var bezierCommand = shape.graphics.bezierCurveTo(500, 200, 200, 500, 500, 500);
var strokeCommand = shape.graphics.strokeColor(Color.Red, 15);
stage.addChild(shape);
stage.juggler.translation(500, 200, 5.0, Transition.sine).listen((v) {
// change "controlX1" of the bezier draw command in an animation
bezierCommand.controlX1 = v.toDouble();
});
stage.juggler.translation(200, 500, 5.0, Transition.sine).listen((v) {
// change "controlX2" of the bezier draw command in an animation
bezierCommand.controlX2 = v.toDouble();
});
stage.juggler.translation(15, 50, 15.0, Transition.sine).listen((v) {
// change "width" of the stroke draw command in an animation
strokeCommand.width = v.toDouble();
});
}
I won't describe how to make the positions of the bezier curve random, that's just a specialized case of the example shown above.

IOS Xamarin - Realtime plot draw lines

I'm working in a cross test project for display ecg graph in realtime. it's work fine on windows and android using Xamarin, BUT in IOS i have slow and slow performarces.
I think the problem is due to my lack of expertise in ios..
I've done two tests both failed, someone have a solutions to speed up ?
TEST A
I call Plot and then assign the UIImage wBitmap as MyView Backgroud every < 10ms
public void Plot(PointF pPrec, PointF pNext)
{
SizeF bitmapSize = new SizeF(wBitmap.Size);
using (CGBitmapContext context2 = new CGBitmapContext(IntPtr.Zero, (int)bitmapSize.Width, (int)bitmapSize.Height, 8, (int)(4 * bitmapSize.Width), CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst))
{
context2.DrawImage(new RectangleF(0, 0, wBitmap.Size.Width, wBitmap.Size.Height), wBitmap.CGImage);
context2.SetLineWidth(1);
context2.AddLineToPoint(pNext.X, pNext.Y);
context2.DrawPath(CGPathDrawingMode.Stroke);
// output the drawing to the view
wBitmap = UIImage.FromImage(context2.ToImage());
}
}
TEST B
I call Plot and then assign the UIImage wBitmap as MyView Backgroud every < 10ms
public void Plot2(PointF pPrec, PointF pNext) { UIGraphics.BeginImageContext(wBitmap.Size); context = UIGraphics.GetCurrentContext();
using (context)
{
if (wBitmap != null)
{
context.TranslateCTM(0f, wBitmap.Size.Height);
context.ScaleCTM(1.0f, -1.0f);
context.DrawImage(new RectangleF(0f, 0f, wBitmap.Size.Width, wBitmap.Size.Height), wBitmap.CGImage);
context.ScaleCTM(1.0f, -1.0f);
context.TranslateCTM(0f, -wBitmap.Size.Height);
}
context.SetLineWidth(1);
context.MoveTo(pPrec.X, pPrec.Y);
context.AddLineToPoint(pNext.X, pNext.Y);
context.DrawPath(CGPathDrawingMode.Stroke);
wBitmap = UIGraphics.GetImageFromCurrentImageContext();
}//end using cont
UIGraphics.EndImageContext();
}
You don't need to draw into a bitmap first and then use that bitmap in a view. The correct approach to this is to subclass UIView and then override the Draw() method and paint your stuff in there.
You can then call SetNeedsDisplay() on the view to schedule a redraw.
The example below draw an arrow shaped background:
class YourView: UIView
{
public override void Draw(RectangleF rect)
{
const float inset = 15f;
UIColor.Blue.SetFill();
var path = new UIBezierPath();
path.MoveTo(new PointF(0, 0));
path.AddLineTo(new PointF(rect.Width - inset, 0));
path.AddLineTo(new PointF(rect.Width, rect.Height * 0.5f));
path.AddLineTo(new PointF(rect.Width - inset, rect.Bottom));
path.AddLineTo(new PointF(0, rect.Bottom));
path.AddLineTo(new PointF(0, 0));
path.Fill();
}
}
}
Alternatively you might want to look at existing components, like CorePlot or TeeChart available in the Xamarin Components store. TeeChart would even be cross platform, so you wouldn't have to worry about Android vs. iOS.

Paint to a bitmap using DirectX under Windows Metro

I am trying to use DirectX to create and paint into a bitmap, and then later use that bitmap to paint to the screen. I have the following test code which I have pieced together from a number of sources. I seem to be able to create the bitmap without errors, and I try and fill it with red. However, when I use the bitmap to paint to the screen I just get a black rectangle. Can anyone tell me what I might be doing wrong? Note that this is running as a Windows 8 Metro app, if that makes a difference.
Note that the formatter seems to be removing my angle brackets and I don't how stop it from doing that. The 'device' variable is of class ID3D11Device, the 'context' variable is ID3D11DeviceContext, the d2dContext variable is ID2D1DeviceContext, the dgixDevice variable is IDXGIDevice, and the 'd2dDevice' is ID2D1Device.
void PaintTestBitmap(GRectF& rect)
{
HRESULT hr;
UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
D3D_FEATURE_LEVEL_9_2,
D3D_FEATURE_LEVEL_9_1
};
ComPtr<ID3D11Device> device;
ComPtr<ID3D11DeviceContext> context;
ComPtr<ID2D1DeviceContext> d2dContext;
hr = D3D11CreateDevice( NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
creationFlags,
featureLevels,
ARRAYSIZE(featureLevels),
D3D11_SDK_VERSION,
&device,
NULL,
&context);
if (SUCCEEDED(hr))
{
ComPtr<IDXGIDevice> dxgiDevice;
device.As(&dxgiDevice);
// create D2D device context
ComPtr<ID2D1Device> d2dDevice;
hr = D2D1CreateDevice(dxgiDevice.Get(), NULL, &d2dDevice);
if (SUCCEEDED(hr))
{
hr = d2dDevice->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2dContext);
if (SUCCEEDED(hr))
{
ID2D1Bitmap1 *pBitmap;
D2D1_SIZE_U size = { rect.m_width, rect.m_height };
D2D1_BITMAP_PROPERTIES1 properties = {{ DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED }, 96, 96, D2D1_BITMAP_OPTIONS_TARGET, 0 };
hr = d2dContext->CreateBitmap(size, (const void *) 0, 0, &properties, &pBitmap);
if (SUCCEEDED(hr))
{
d2dContext->SetTarget(pBitmap);
d2dContext->BeginDraw();
d2dContext->Clear(D2D1::ColorF(D2D1::ColorF::Red)); // Clear the bitmap to Red
d2dContext->EndDraw();
// If I now use pBitmap to paint to the screen, I get a black rectange
// rather than the red one I was expecting
}
}
}
}
}
Note that I am fairly new to both DirectX and Metro style apps.
Thanks
what you're probably forgetting is that you dont draw your bitmap on your context.
what you can try is
hr = d2dContext->CreateBitmap(size, (const void *) 0, 0, &properties, &pBitmap);
if (SUCCEEDED(hr))
{
ID2D1Image *pImage = nullptr;
d2dContext->GetTarget(&image);
d2dContext->SetTarget(pBitmap);
d2dContext->BeginDraw();
d2dContext->Clear(D2D1::ColorF(D2D1::ColorF::Red)); // Clear the bitmap to Red
d2dContext->SetTarget(pImage);
d2dContext->DrawBitmap(pBitmap);
d2dContext->EndDraw();
}
what you do is store your rendertarget and then draw your bitmap and at the end you set your context to the right render target and let it draw your bitmap on it

Resources