简体   繁体   English

动态更改笔触颜色

[英]Changing stroke color dynamically

I am using stylus input to draw lines in a canvas.我正在使用手写笔输入在画布中绘制线条。 I want to change the color of the stroke with the pen pressure.我想用笔压力改变笔触的颜色。 so I used:所以我用:

DrawingAttributes dattribute = new DrawingAttributes();
inkcan.EditingMode = InkCanvasEditingMode.Ink;

if (stylusInput.pressureFactor < 0.5)
dattribute.Color = Colors.Red;
else
dattribute.Color = Colors.Blue;

inkcan.DefaultDrawingAttributes = dattribute;

but I have found that the color changes only when I lift off and retouch the pen to tablet surface.但我发现只有当我抬起笔并将笔润饰到数位板表面时,颜色才会改变。 I am not sure how to fix that problem.我不知道如何解决这个问题。

Any help would be greatly appreciated.任何帮助将不胜感激。

Look at this question: InkCanvas Eraser看这个问题: InkCanvas Eraser

In the MSDN it states:在 MSDN 中,它指出:

If you change the EraserShape, the cursor rendered on the InkCanvas is not updated until the next EditingMode change.如果更改 EraserShape,则在下一次 EditingMode 更改之前不会更新 InkCanvas 上呈现的光标。

The effect you are experiencing might be caused by the EditingMode being changed when you pull the pen off the canvas and put it back down.当您将笔从画布上拉出并放回原处时,您遇到的效果可能是由于 EditingMode 发生了变化。

If so, you could toggle the EditingMode property as I suggested in the linked answer.如果是这样,您可以按照我在链接答案中的建议切换 EditingMode 属性。

EDIT编辑

Have a look at this a 3rd down it says: 看看这个第三个下来它说:

Off course, life is never as simple as that, so there is one more little issue to handle.当然,生活从来没有这么简单,所以还有一个小问题要处理。 Apparently, the InkCanvas uses different renderers for the final result compared to while the strokes are being drawn.显然,与绘制笔划时相比,InkCanvas 对最终结果使用了不同的渲染器。 To show a transparency based on the pressure while the drawing action is still in progress, we need to use the protected property called DyamicRenderer which gets/sets the object used to render the strokes on a drawing context while the stroke is being drawn.为了在绘图操作仍在进行时基于压力显示透明度,我们需要使用名为 DyamicRenderer 的受保护属性,该属性获取/设置用于在绘制笔画时在绘图上下文上呈现笔画的对象。 This rendering object must be a descendent of DynamicRenderer.此渲染对象必须是 DynamicRenderer 的后代。 All you need to do here is override the OnDraw method and change the brush that is used.在这里您需要做的就是覆盖 OnDraw 方法并更改使用的画笔。 When you assign a new value to this property, the InkCanvas actually changes an internal 'PlugIn list' which is called whenever data is entered using the stylus.当您为此属性分配新值时,InkCanvas 实际上会更改内部“插件列表”,每当使用触控笔输入数据时都会调用该列表。

This might be it.这可能是它。

The if condition is evaluated only once, so there is no reason for the colour to change while you are drawing. if 条件只计算一次,因此在绘制时没有理由改变颜色。 Unfortunately, there seems to be no "onpressurechanged" event, so you would probably have to set up a loop that checks the pressure every x milliseconds and adjusts the colour accordingly.不幸的是,似乎没有“onpressurechanged”事件,因此您可能必须设置一个循环,每 x 毫秒检查一次压力并相应地调整颜色。 Since you don't want to freeze the UI, you would either need to run it in another thread and delegate the colour change back to the UI thread, or you can queue the pressure checks onto the window dispatcher with "applicationIdle" priority.由于您不想冻结 UI,您需要在另一个线程中运行它并将颜色更改委托回 UI 线程,或者您可以将压力检查排队到具有“applicationIdle”优先级的窗口调度程序上。 This would look something like:这看起来像:

void checkPressure(InkCanvas inkcan)
{
//return if touch is lifted code here

DrawingAttributes dattribute = new DrawingAttributes();
if (stylusInput.pressureFactor < 0.5)
dattribute.Color = Colors.Red;
else
dattribute.Color = Colors.Blue;
inkcan.DefaultDrawingAttributes = dattribute;
this.Dispatcher.BeginInvoke(new MyPressureDelegate(checkPressure), DispatcherPriority.ApplicationIdle, inkcan);
}

assuming your stylusInput shares scope with the function, of course.当然,假设您的 stylusInput 与函数共享范围。 Otherwise you'd need to pass it in along with the canvas in an object array.否则,您需要将它与对象数组中的画布一起传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM