Skip to content

Commit 3ed2f1c

Browse files
committed
merged in pull request from original repo "Added OnClick and OnMouse actions for Canvas BlazorExtensions#84"
1 parent 8523b22 commit 3ed2f1c

14 files changed

+121
-21
lines changed
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
@inherits BECanvasComponent
22

3-
<canvas id="@Id" width="@Width" height="@Height" @ref="_canvasRef"></canvas>
3+
<canvas id="@Id" width="@Width" height="@Height" @ref="_canvasRef" onclick="@OnClick" onmousedown="@OnMouseDown" onmousemove="@OnMouseMove" onmouseout="@OnMouseOut" onmouseover="@OnMouseOver" onmouseup="@OnMouseUp" onmousewheel="@OnMouseWheel"></canvas>

src/Blazor.Extensions.Canvas/BECanvasComponent.cs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.Components;
2+
using Microsoft.AspNetCore.Components.Web;
23
using Microsoft.JSInterop;
34
using System;
45

@@ -12,6 +13,27 @@ public class BECanvasComponent : ComponentBase
1213
[Parameter]
1314
public long Width { get; set; }
1415

16+
[Parameter]
17+
public Action<MouseEventArgs> OnClick { get; set; }
18+
19+
[Parameter]
20+
public Action<MouseEventArgs> OnMouseDown { get; set; }
21+
22+
[Parameter]
23+
public Action<MouseEventArgs> OnMouseMove { get; set; }
24+
25+
[Parameter]
26+
public Action<MouseEventArgs> OnMouseOut { get; set; }
27+
28+
[Parameter]
29+
public Action<MouseEventArgs> OnMouseOver { get; set; }
30+
31+
[Parameter]
32+
public Action<MouseEventArgs> OnMouseUp { get; set; }
33+
34+
[Parameter]
35+
public Action<MouseEventArgs> OnMouseWheel { get; set; }
36+
1537
protected readonly string Id = Guid.NewGuid().ToString();
1638
protected ElementReference _canvasRef;
1739

@@ -20,4 +42,4 @@ public class BECanvasComponent : ComponentBase
2042
[Inject]
2143
internal IJSRuntime JSRuntime { get; set; }
2244
}
23-
}
45+
}

src/Blazor.Extensions.Canvas/Blazor.Extensions.Canvas.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@
4040
</Content>
4141
</ItemGroup>
4242

43-
</Project>
43+
</Project>

src/Blazor.Extensions.Canvas/Canvas2D/Canvas2DContext.cs

+14-8
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ public class Canvas2DContext : RenderingContext
6161
"repeat", "repeat-x", "repeat-y", "no-repeat"
6262
};
6363

64+
private const string GET_IMAGE_DATA_METHOD = "getImageData";
65+
private const string PUT_IMAGE_DATA_METHOD = "putImageData";
6466
#endregion
6567

6668
#region Properties
6769

68-
public object FillStyle { get; private set; } = "#000";
70+
public string FillStyle { get; private set; } = "#000";
6971

7072
public string StrokeStyle { get; private set; } = "#000";
7173

@@ -96,7 +98,6 @@ public class Canvas2DContext : RenderingContext
9698
public float ShadowOffsetY { get; private set; }
9799

98100
public float GlobalAlpha { get; private set; } = 1.0f;
99-
100101
public string GlobalCompositeOperation { get; private set; } = "source-over";
101102

102103
#endregion Properties
@@ -107,10 +108,10 @@ public Canvas2DContext(BECanvasComponent reference) : base(reference, CONTEXT_NA
107108

108109
#region Property Setters
109110

110-
public async Task SetFillStyleAsync(object value)
111+
public async Task SetFillStyleAsync(string value)
111112
{
112113
this.FillStyle = value;
113-
await this.BatchCallAsync(FILL_STYLE_PROPERTY, false, value);
114+
await this.BatchCallAsync(FILL_STYLE_PROPERTY, isMethodCall: false, value);
114115
}
115116

116117
public async Task SetStrokeStyleAsync(string value)
@@ -202,7 +203,6 @@ public async Task SetGlobalAlphaAsync(float value)
202203
this.GlobalAlpha = value;
203204
await this.BatchCallAsync(GLOBAL_ALPHA_PROPERTY, isMethodCall: false, value);
204205
}
205-
206206
public async Task SetGlobalCompositeOperationAsync(string value)
207207
{
208208
this.GlobalCompositeOperation = value;
@@ -212,7 +212,6 @@ public async Task SetGlobalCompositeOperationAsync(string value)
212212
#endregion Property Setters
213213

214214
#region Methods
215-
216215
[Obsolete("Use the async version instead, which is already called internally.")]
217216
public void FillRect(double x, double y, double width, double height) => this.CallMethod<object>(FILL_RECT_METHOD, x, y, width, height);
218217
public async Task FillRectAsync(double x, double y, double width, double height) => await this.BatchCallAsync(FILL_RECT_METHOD, isMethodCall: true, x, y, width, height);
@@ -337,12 +336,19 @@ public async Task SetGlobalCompositeOperationAsync(string value)
337336
public void Restore() => this.CallMethod<object>(RESTORE_METHOD);
338337
public async Task RestoreAsync() => await this.BatchCallAsync(RESTORE_METHOD, isMethodCall: true);
339338

339+
[Obsolete("Use the async version instead, which is already called internally.")]
340+
public ImageData GetImageData(double sx, double sy, double sh, double sw) => this.CallMethod<ImageData>(GET_IMAGE_DATA_METHOD, sx, sy, sh, sw);
341+
public async Task<ImageData> GetImageDataAsync(double sx, double sy, double sh, double sw) => await this.CallMethodAsync<ImageData>(GET_IMAGE_DATA_METHOD, sx, sy, sh, sw);
342+
343+
[Obsolete("Use the async version instead, which is already called internally.")]
344+
public void PutImageData(ImageData imageData, double dx, double dy) => this.CallMethod<object>(PUT_IMAGE_DATA_METHOD, imageData, dx, dy);
345+
public async Task PutImageDataAsync(ImageData imageData, double dx, double dy) => await this.CallMethodAsync<object>(PUT_IMAGE_DATA_METHOD, imageData, dx, dy);
346+
340347
public async Task DrawImageAsync(ElementReference elementReference, double dx, double dy) => await this.BatchCallAsync(DRAW_IMAGE_METHOD, isMethodCall: true, elementReference, dx, dy);
341348
public async Task DrawImageAsync(ElementReference elementReference, double dx, double dy, double dWidth, double dHeight) => await this.BatchCallAsync(DRAW_IMAGE_METHOD, isMethodCall: true, elementReference, dx, dy, dWidth, dHeight);
342349
public async Task DrawImageAsync(ElementReference elementReference, double sx, double sy, double sWidth, double sHeight, double dx, double dy, double dWidth, double dHeight) => await this.BatchCallAsync(DRAW_IMAGE_METHOD, isMethodCall: true, elementReference, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);
343-
344350
public async Task<object> CreatePatternAsync(ElementReference image, RepeatPattern repeat) => await this.CallMethodAsync<object>(CREATE_PATTERN_METHOD, image, this._repeatNames[(int)repeat]);
345351

346352
#endregion Methods
347353
}
348-
}
354+
}

src/Blazor.Extensions.Canvas/Canvas2D/Canvas2DEnums.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ public enum LineJoin
3939
Round,
4040
Bevel
4141
}
42-
4342
public enum RepeatPattern
4443
{
4544
Repeat = 0,
4645
RepeatX,
4746
RepeatY,
4847
NoRepeat
4948
}
50-
}
49+
50+
}

src/Blazor.Extensions.Canvas/CanvasContextExtensions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public static async Task<WebGLContext> CreateWebGLAsync(this BECanvasComponent c
3535
{
3636
return await new WebGLContext(canvas, attributes).InitializeAsync().ConfigureAwait(false) as WebGLContext;
3737
}
38-
3938
public static async Task<WebGL2Context> CreateWebGL2Async(this BECanvasComponent canvas, WebGLContextAttributes attributes = null)
4039
{
4140
return await new WebGL2Context(canvas, attributes).InitializeAsync().ConfigureAwait(false) as WebGL2Context;
4241
}
42+
4343
}
44-
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Buffers;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Text.Json;
7+
using System.Text.Json.Serialization;
8+
9+
namespace Blazor.Extensions.Canvas.Infrastructure
10+
{
11+
class ImageDataArrayConverter : JsonConverter<byte[]>
12+
{
13+
public override byte[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
14+
{
15+
Dictionary<string, byte> dictionary = JsonSerializer.Deserialize<Dictionary<string, byte>>(ref reader, options);
16+
return dictionary.Values.ToArray<byte>();
17+
}
18+
19+
public override void Write(Utf8JsonWriter writer, byte[] value, JsonSerializerOptions options)
20+
{
21+
writer.WriteStartObject();
22+
for (int i = 0; i < value.Length; i++)
23+
{
24+
writer.WritePropertyName(i.ToString());
25+
JsonSerializer.Serialize(writer, value[i], options);
26+
}
27+
writer.WriteEndObject();
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Blazor.Extensions.Canvas.Model;
2+
using System;
3+
using System.Buffers;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Text.Json;
8+
using System.Text.Json.Serialization;
9+
10+
namespace Blazor.Extensions.Canvas.Infrastructure
11+
{
12+
class ImageDataConverter : JsonConverter<ImageData>
13+
{
14+
public override ImageData Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
15+
{
16+
return JsonSerializer.Deserialize<ImageData>(ref reader, options);
17+
}
18+
19+
public override void Write(Utf8JsonWriter writer, ImageData value, JsonSerializerOptions options)
20+
{
21+
JsonSerializer.Serialize(writer, value, options);
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Blazor.Extensions.Canvas.Infrastructure;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using System.Text.Json.Serialization;
6+
7+
namespace Blazor.Extensions.Canvas.Model
8+
{
9+
public class ImageData
10+
{
11+
[JsonPropertyName("data")]
12+
[JsonConverter(typeof(ImageDataArrayConverter))]
13+
public byte[] Data { get; set; }
14+
}
15+
}

src/Blazor.Extensions.Canvas/RenderingContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,4 @@ public void Dispose()
135135

136136
#endregion
137137
}
138-
}
138+
}

src/Blazor.Extensions.Canvas/WebGL/WebGLContext.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,10 @@ public void Uniform(WebGLUniformLocation location, params float[] value)
573573
throw new ArgumentOutOfRangeException(nameof(value), value.Length, "Value array is empty or too long");
574574
}
575575
}
576+
public async Task Uniform1Async(WebGLUniformLocation location, params float[] value)
577+
{
578+
await this.BatchCallAsync(UNIFORM + "1fv", isMethodCall: true, location, value);
579+
}
576580
public async Task UniformAsync(WebGLUniformLocation location, params float[] value)
577581
{
578582
switch (value.Length)
@@ -748,4 +752,4 @@ private byte[] ConvertToByteArray<T>(T[] arr) where T : unmanaged
748752
private async Task<int> GetDrawingBufferHeightAsync() => await this.GetPropertyAsync<int>(DRAWING_BUFFER_HEIGHT);
749753
#endregion
750754
}
751-
}
755+
}

src/Blazor.Extensions.Canvas/WebGL/WebGLEnums.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,6 @@ public enum TextureParameterValue
319319
CLAMP_TO_EDGE = 0x812F,
320320
MIRRORED_REPEAT = 0x8370
321321
}
322-
323322
public enum BlitFilter
324323
{
325324
NEAREST = 0x2600,
@@ -469,4 +468,4 @@ public enum Texture
469468
TEXTURE30,
470469
TEXTURE31
471470
}
472-
}
471+
}

src/Blazor.Extensions.Canvas/WebGL/WebGLObjects.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class WebGLShader : WebGLObject
5656

5757
public class WebGLUniformLocation : WebGLObject
5858
{ }
59-
6059
public class WebGLVertexArrayObject : WebGLObject
6160
{ }
61+
6262
}

0 commit comments

Comments
 (0)