Skip to content

Commit

Permalink
- add examples when cropping images
Browse files Browse the repository at this point in the history
- add generation default values of method parameters for API
  • Loading branch information
MaxymGorn committed Nov 13, 2023
1 parent 9f40e96 commit b1290bf
Show file tree
Hide file tree
Showing 25 changed files with 335 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/Cropper.Blazor/Client/Components/Docs/DocsApi.razor
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
</MudTd>
<MudTd Class="docs-content-api-cell" DataLabel="Default">
@{
var def = PresentDefaultValue(context.Default);
var def = context.Default.PresentDefaultValue();
}
@if (def.Contains("<path"))
{
Expand Down
35 changes: 1 addition & 34 deletions src/Cropper.Blazor/Client/Components/Docs/DocsApi.razor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Globalization;
using System.Reflection;
using System.Reflection;
using Cropper.Blazor.Client.Models;
using Cropper.Blazor.Events;
using Cropper.Blazor.Models;
Expand Down Expand Up @@ -257,38 +256,6 @@ private object GetDefaultValue(PropertyInfo info)
return info.GetValue(_comp_instance);
}

DefaultConverter<object> _converter = new DefaultConverter<object>()
{
Culture = CultureInfo.InvariantCulture
};

private string PresentDefaultValue(object value)
{
if (value == null)
return "null";
if (value.GetType() == typeof(string))
{
if (value.ToString() == string.Empty)
{
return "";
}
else
{
return $"\"{value}\"";
}
}
if (value.GetType().IsEnum)
return $"{value.GetType().Name}.{value}";
if (Nullable.GetUnderlyingType(value.GetType()) != null)
return _converter.Set(value);
if (value.GetType().IsGenericType) // for instance event callbacks
return "";
if (value.GetType().IsValueType)
return _converter.Set(value);
return "";
}


#region Grouping properties

private enum Grouping { Categories, Inheritance, None }
Expand Down
3 changes: 2 additions & 1 deletion src/Cropper.Blazor/Client/Cropper.Blazor.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.13" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.13" PrivateAssets="all" />
<PackageReference Include="Blazored.LocalStorage" Version="4.4.0" />
<PackageReference Include="MudBlazor" Version="6.11.0" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -184,6 +183,8 @@
</ItemGroup>
<ItemGroup>
<None Remove="Pages\CropZoom\Examples\MinMaxZoomRatio_ScriptCode.html" />
<None Remove="Pages\Crop\Examples\CropPolygonImage_ScriptCode.html" />
<None Remove="Pages\Crop\Examples\CropRoundImage_ScriptCode.html" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Cropper.Blazor.Shared\Cropper.Blazor.Shared.csproj" />
Expand Down
9 changes: 6 additions & 3 deletions src/Cropper.Blazor/Client/Pages/About.razor
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
<MudCard Elevation="4">
<MudCardHeader>
<CardHeaderAvatar>
<MudAvatar Image="@teamMember.Avatar"
Size="Size.Large"
Class="mud-elevation-4" />
<MudAvatar Size="Size.Large"
Class="mud-elevation-4">
<MudImage UserAttributes="@AvatarInputAttributes" Src="@teamMember.Avatar">

</MudImage>
</MudAvatar>
</CardHeaderAvatar>
<CardHeaderContent>
<MudText Typo="Typo.h6">
Expand Down
7 changes: 7 additions & 0 deletions src/Cropper.Blazor/Client/Pages/About.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,12 @@ public partial class About
Languages = "English, Ukrainian"
}
};

public Dictionary<string, object> AvatarInputAttributes { get; set; } =
new Dictionary<string, object>()
{
{ "loading", "lazy" },
{ "alt", "GitHub avatar image" }
};
}
}
18 changes: 18 additions & 0 deletions src/Cropper.Blazor/Client/Pages/Crop/CropPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,23 @@
</SectionContent>
</DocsPageSection>

<DocsPageSection>
<SectionHeader Title="Crop a round image">

</SectionHeader>
<SectionContent Codes="@(new[] {new CodeFile("Cropper.razor", "CropRoundImageExample"), new CodeFile("script.js", "CropRoundImage_Script")})">
<CropRoundImageExample />
</SectionContent>
</DocsPageSection>

<DocsPageSection>
<SectionHeader Title="Crop a polygon image">

</SectionHeader>
<SectionContent Codes="@(new[] {new CodeFile("Cropper.razor", "CropPolygonImageExample"), new CodeFile("script.js", "CropPolygonImage_Script")})">
<CropPolygonImageExample />
</SectionContent>
</DocsPageSection>

</DocsPageContent>
</DocsPage>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@using Cropper.Blazor.Models;

<div class="img-container">
<CropperComponent Class="big-img" Src="images/Rivne.jpg" @ref="cropperComponent" Options="new Blazor.Models.Options()" />
<CropperComponent Class="big-img" Src="images/budir-church-bu-akirkja-iceland.jpg" @ref="cropperComponent" Options="new Blazor.Models.Options()" />
</div>

<div class="button" @onclick="GetCroppedCanvasAsync">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@using Cropper.Blazor.Models;

<div class="img-container">
<CropperComponent Class="big-img" Src="images/Rivne.jpg" @ref="cropperComponent" Options="new Blazor.Models.Options()" />
<CropperComponent Class="big-img" Src="images/budir-church-bu-akirkja-iceland.jpg" @ref="cropperComponent" Options="new Blazor.Models.Options()" />
</div>

<div class="button" @onclick="GetCroppedCanvasDataURLAsync">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
@using Cropper.Blazor.Extensions;
@using Cropper.Blazor.Models;

<div class="img-container cropper-face-pentagon">
<CropperComponent Class="big-img" Src="images/Mushrooms.jpg" @ref="cropperComponent" Options="new Blazor.Models.Options()" />
</div>

<div class="button" @onclick="GetCroppedCanvasAsync">
Get cropped image
</div>

<img class="cropped-img-container" src="@croppedCanvasDataURL" />

@* Make sure the size of the image fits perfectly into the container *@
<style>
.cropper-face {
opacity: 25%;
}
.img-container.cropper-face-pentagon .cropper-container .cropper-crop-box .cropper-face {
clip-path: polygon(50% 0%, 100% 38%, 82% 100%, 18% 100%, 0% 38%);
}
.big-img {
max-height: 400px;
/* This rule is very important, please don't ignore this */
max-width: 100%;
}
.img-container {
max-height: 400px;
width: 100%;
}
/* Means that the cropped image will take up 100% of the width of its containing element */
.cropped-img-container {
width: 100%;
}
/* These styles are just needed for a nice button and don't related with cropper component */
.button {
display: inline-block;
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
text-align: center;
text-decoration: none;
font-size: 16px;
cursor: pointer;
}
</style>

@code {
[Inject] private IJSRuntime? JSRuntime { get; set; }

private CropperComponent? cropperComponent = null!;
private string croppedCanvasDataURL;

public async Task GetCroppedCanvasAsync()
{
GetCroppedCanvasOptions getCroppedCanvasOptions = new GetCroppedCanvasOptions
{
MaxHeight = 4096,
MaxWidth = 4096,
ImageSmoothingQuality = ImageSmoothingQuality.High.ToEnumString()
};

// Get a reference to a JavaScript cropped canvas object.
CroppedCanvas croppedCanvas = await cropperComponent!.GetCroppedCanvasAsync(getCroppedCanvasOptions);
// Invoke toDataURL JavaScript function from the canvas object.
croppedCanvasDataURL = await JSRuntime!.InvokeAsync<string>(
"window.addClipPathPolygon",
croppedCanvas!.JSRuntimeObjectRef,
// Defines a polygon using an SVG fill rule and a set of vertices previously defined in the following format styles:
// .img-container.cropper-face-pentagon.cropper-container.cropper-crop-box.cropper-face {
// clip-path: polygon(50% 0%, 100% 38%, 82% 100%, 18% 100%, 0% 38%);
// }
// In our case, we need to pass the same data, but without the percent sign (%)
new int[] { 50, 0, 100, 38, 82, 100, 18, 100, 0, 38 });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@namespace Cropper.Blazor.Client.Pages.Crop.Examples

@* Dummy file do not remove, needed for content section *@
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<div class="mud-codeblock">
<div class="html">
<pre>
<span class="htmlTagDelimiter">&lt;</span><span class="htmlElementName">script</span><span class="htmlTagDelimiter">&gt;</span>
window.addClipPathPolygon = (sourceCanvas, path) => {
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
const width = sourceCanvas.width,
height = sourceCanvas.height;

canvas.width = width;
canvas.height = height;
context.imageSmoothingEnabled = true;

context.beginPath();
context.moveTo(path[0] * width / 100, path[1] * height / 100);
context.fillStyle = "rgba(255, 255, 255, 0)";

for (let i = 2; i < path.length; i += 2) {
context.lineTo(path[i] * width / 100, path[i + 1] * height / 100);
}

context.closePath();
context.clip();
context.fill();
context.globalCompositeOperation = 'lighter';
context.drawImage(sourceCanvas, 0, 0, width, height);

return canvas.toDataURL("image/png", 1);
}
<span class="htmlTagDelimiter">&lt;&#47;</span><span class="htmlElementName">script</span> <span class="htmlTagDelimiter">&gt;</span>
</pre>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
@using Cropper.Blazor.Extensions;
@using Cropper.Blazor.Models;

<div class="img-container cropper-face-circle">
<CropperComponent Class="big-img" Src="images/lone-tree.jpg" @ref="cropperComponent" Options="new Blazor.Models.Options()" />
</div>

<div class="button" @onclick="GetCroppedCanvasAsync">
Get cropped image
</div>

<img class="cropped-img-container" src="@croppedCanvasDataURL" />

@* Make sure the size of the image fits perfectly into the container *@
<style>
.cropper-face {
opacity: 25%;
}
.img-container.cropper-face-circle .cropper-container .cropper-crop-box .cropper-face {
border-radius: 50%;
}
.big-img {
max-height: 400px;
/* This rule is very important, please don't ignore this */
max-width: 100%;
}
.img-container {
max-height: 400px;
width: 100%;
}
/* Means that the cropped image will take up 100% of the width of its containing element */
.cropped-img-container {
width: 100%;
}
/* These styles are just needed for a nice button and don't related with cropper component */
.button {
display: inline-block;
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
text-align: center;
text-decoration: none;
font-size: 16px;
cursor: pointer;
}
</style>

@code {
[Inject] private IJSRuntime? JSRuntime { get; set; }

private CropperComponent? cropperComponent = null!;
private string croppedCanvasDataURL;

public async Task GetCroppedCanvasAsync()
{
GetCroppedCanvasOptions getCroppedCanvasOptions = new GetCroppedCanvasOptions
{
MaxHeight = 4096,
MaxWidth = 4096,
ImageSmoothingQuality = ImageSmoothingQuality.High.ToEnumString()
};

// Get a reference to a JavaScript cropped canvas object.
CroppedCanvas croppedCanvas = await cropperComponent!.GetCroppedCanvasAsync(getCroppedCanvasOptions);
// Invoke toDataURL JavaScript function from the canvas object.
croppedCanvasDataURL = await JSRuntime!.InvokeAsync<string>("window.addClipPathEllipse", croppedCanvas!.JSRuntimeObjectRef);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@namespace Cropper.Blazor.Client.Pages.Crop.Examples

@* Dummy file do not remove, needed for content section *@
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<div class="mud-codeblock">
<div class="html">
<pre>
<span class="htmlTagDelimiter">&lt;</span><span class="htmlElementName">script</span><span class="htmlTagDelimiter">&gt;</span>
window.addClipPathEllipse = (sourceCanvas) => {
const createdCanvas = document.createElement('canvas');
const contextCanvas = createdCanvas.getContext('2d');
const widthCanvas = sourceCanvas.width,
heightCanvas = sourceCanvas.height;

createdCanvas.width = widthCanvas;
createdCanvas.height = heightCanvas;
contextCanvas.imageSmoothingEnabled = true;

contextCanvas.drawImage(sourceCanvas, 0, 0, widthCanvas, heightCanvas);
contextCanvas.globalCompositeOperation = 'destination-in';
contextCanvas.beginPath();
contextCanvas.ellipse(widthCanvas / 2, heightCanvas / 2, widthCanvas / 2, heightCanvas / 2, 0 * Math.PI, 0, 180 * Math.PI, true);
contextCanvas.fill();

return createdCanvas.toDataURL("image/png", 1);
}
<span class="htmlTagDelimiter">&lt;&#47;</span><span class="htmlElementName">script</span> <span class="htmlTagDelimiter">&gt;</span>
</pre>
</div>
</div>
3 changes: 2 additions & 1 deletion src/Cropper.Blazor/Client/Pages/CropperDemo.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public partial class CropperDemo : IDisposable
new Dictionary<string, object>()
{
{ "loading", "lazy" },
{ "test-Attribute", "123-test" }
{ "test-Attribute", "123-test" },
{ "alt", "Cropper.Blazor demo image" }
};

protected override void OnInitialized()
Expand Down
2 changes: 1 addition & 1 deletion src/Cropper.Blazor/Client/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<MudGrid Class="mt-5" Justify="Justify.Center">

<MudItem xl="12" lg="12" md="12" sm="12" xs="12" Class="d-flex justify-center">
<MudImage Src="cropperblazor.png" Fluid="true" Width="256" Height="256" />
<MudImage Src="cropperblazor.png" UserAttributes="@LogoInputAttributes" Fluid="true" Width="256" Height="256" />
</MudItem>

<MudItem xl="12" lg="12" md="12" sm="12" xs="12" Class="d-flex justify-center">
Expand Down
Loading

0 comments on commit b1290bf

Please sign in to comment.